Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do you make 'readLocalData("something", table)' return the table?

I am trying to use the code below to make 'haircColors' give me the following table, but it keeps saying that it is a string and I cannot insert any other values in it :(
Could anyone please help me?

--~~
hairColors= readLocalData( "hairColors" , {color(64, 48, 37, 255), color(102, 78, 50, 255), color(0, 0, 0, 255), color(70, 34, 30, 255), color(184, 161, 125, 255)})

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 7,809

    @hohohohoho What exactly are you trying to do. If your trying to read something from local data, then you need to save something to local data first. You can only save a string or number or nil to local data. Or are you just trying to create a table hairColors with those values.

        hairColors={color(64, 48, 37, 255), color(102, 78, 50, 255), color(0, 0, 0, 255), color(70, 34, 30, 255), color(184, 161, 125, 255)}
    
  • @dave1707 Well I am trying to create a table that would hold values forever, one that I would be able to remove some values and add in more at any time.

  • IgnatzIgnatz Mod
    Posts: 5,396

    Search the forum for serialise

  • dave1707dave1707 Mod
    Posts: 7,809

    @hohohohoho Using the table I show above, you can add new entries or remove existing entries. Look in the Codea reference under Lua tables for the functions that can be used with tables.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @dave1707 - he wants to save the table in local data and retrieve it later. That's why I suggested looking for serialise, because this has been discussed many times.

  • dave1707dave1707 Mod
    Posts: 7,809

    @Ignatz That's why my first question was to ask him what he was trying to do. His response was he wanted to create a table that he could remove and add values. It's hard to give a good answer when you don't get a good question. So if he wants to save a table in local data, then the answer is read the Codea reference for json.encode and json.decode.

  • IgnatzIgnatz Mod
    edited March 2016 Posts: 5,396

    @dave1707 - he said he wanted a table that would hold values forever, ie one he can store and recover.

  • Posts: 41

    @hohohohoho: If I understand you right you want to save/load tables. This isn't directly possible. You need to convert tables to strings to save and later load the string and convert it back to a table. Here's some code I'm using for this:

    -- table.toString and string.toTable: ------------------------------------------
    --[[
    Courtesy of Briarfox, 2013
    http://codea.io/talk/discussion/3797/save-load-table-to-and-from-a-string
    Modified by HeiKoDea
    
    Example:
    -- Test table:
    tbl1 = {10,11,12,date={Year="2013",Month={"Oct","10"},Day="14"}}
    
    -- Convert table to string:
    str = table.toString(tbl1)
    print("String is: "..str)
    
    --string.toTable():
    test1 = string.toTable(str)
    print("Year is: "..test1.date.Year)
    
    --toTable using self:
    print("Month is: "..str:toTable().date.Month[1].." "..str:toTable().date.Month[2])
    --]]
    
    function table.val_to_str (v)
        if "string" == type(v) then
            v = string.gsub(v, "\n", "\\n")
            if string.match(string.gsub(v, "[^'\"]", ""), '^"+$') then
                return "'"..v.."'"
            end
            return '"'..string.gsub(v, '"', '\\"')..'"'
        else
            return "table" == type(v) and table.toString(v) or tostring(v)
        end
    end
    
    function table.key_to_str (k)
        if "string" == type(k) and string.match(k, "^[_%a][_%a%d]*$") then
            return k
        else
            return "["..table.val_to_str(k).."]"
        end
    end
    
    function table.toString(tbl)
        local result, done = {}, {}
        for k, v in ipairs(tbl) do
            table.insert(result, table.val_to_str(v))
            done[k] = true
        end
        for k, v in pairs(tbl) do
            if not done[k] then
                table.insert(result, table.key_to_str(k).."="..table.val_to_str(v))
            end
        end
        return "{"..table.concat(result, ",").."}"
    end
    
    function string.toTable(self)
        local t = loadstring("return "..self)
        return assert(t(),"Can not convert string to table.")
    end
    
  • dave1707dave1707 Mod
    edited March 2016 Posts: 7,809

    @hohohohoho Since all you want to do is save colors that are in a table to memory (project), this will work. Just use whichever function to do what you want.

    function setup()
        col={}  -- table for colors
    
        -- Use this the first time to create and save the color table
        col={ {64, 48, 37, 255}, 
              {102, 78, 50, 255}, 
              {0, 0, 0, 255}, 
              {70, 34, 30, 255}, 
              {184, 161, 125, 255} }
        saveTable()
    
        -- use this to read the table from memory and create the color table
        readTable()
    
        -- use this to add more colors to the color table
        updateTable(255,0,0,255)
    
        -- use this to remove a color position from the color table
        removeTable(3)
    
        -- use this to save the color table to project memory
        saveTable()
    end
    
    function draw()
        background(0)
        for z=1,#col do   -- draw circles showing the colors in the table
            fill(col[z])
            ellipse(250,HEIGHT-50*z,45)
        end
    end
    
    function saveTable()
        local temp={}
        for z=1,#col do
            temp[z]={col[z][1],col[z][2],col[z][3],col[z][4]}
        end
        saveProjectData("colorTab",json.encode(temp))
    end
    
    function readTable()
        local hc=json.decode(readProjectData("colorTab"))
        for t=1,#hc do
            col[t]=color(hc[t][1],hc[t][2],hc[t][3],hc[t][4])
        end    
    end
    
    function updateTable(r,g,b,a)
        table.insert(col,color(r,g,b,a))
    end
    
    function removeTable(pos)
        table.remove(col,pos)
    end
    
  • Posts: 116

    Why dont use json?

    tab = {a=25, b=139, c =550}
    --##saving##--
    local tabjson = json.encode(tab)
    saveLocalData("myTable", tabjson)
    --##reading##--
    local tabjson = readLocalData("myTable")
    tab = json.decode(tabjson)
    
  • dave1707dave1707 Mod
    Posts: 7,809

    @TimurEke The code just above yours uses json encode and decode.

  • Posts: 116

    @dave1707 yup! Have overseen that :D

Sign In or Register to comment.