Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

small table with index


Question

Posted

I want to build an array with data as in local tb ={1=a,2=b,3=c} and so on.

 

one question,

1 - I notice that i cant add a number as the index as above? unless i am doing something wrong?

 

Recommended Posts

  • 0
Posted
54 minutes ago, Jay Ess said:

I want to build an array with data as in local tb ={1=a,2=b,3=c} and so on.

 

one question,

1 - I notice that i cant add a number as the index as above? unless i am doing something wrong?

 

 

tb ={[1]=a,[2]=b,[3]=c}

 

The reason is  that number indexes are computed, and then you  need to put  it in [...] that computes the index.

tb = {["prefix"..index] =  42} is another example of a computed prefix.

 

tb = {["foo"] =  42} is the same as tb={foo=42}

However, using the string directly as  a key as in the last  example only works if the string is a valid key name (similar  to variable name), so no  spaces or language specific letters.

 

 

  • Like 1
  • 0
  • Inquirer
  • Posted

    i notice is i index them as [1] etc then if i jump a number it does not add it to the table, correct? how can i get around that?

    • 0
    Posted
    18 minutes ago, Jay Ess said:

    i notice is i index them as [1] etc then if i jump a number it does not add it to the table, correct? how can i get around that?

    I  don't understand -  can you give  example?

    • 0
  • Inquirer
  • Posted

    local tb ={[1]=a,[2]=b,[3]=c,[23]=d}

    when i do a print of k,v it only shows me 1-3 and not 23

    i assume that is because the index has to be in order? so how would i get around that? other then keeping them in order?

    • 0
    Posted

    Please login or register to see this code.

     

    use pairs

    • 0
  • Inquirer
  • Posted (edited)

    thank you @jgab

    i have 56 values in my array, how do i fetch back value 23 please?

    Edited by Jay Ess
    • 0
    Posted

    a=tb[23]
    or

    print(tb[23])

    • 0
  • Inquirer
  • Posted

    thank you and to get the index if i know the value?

    • 0
    Posted
    6 hours ago, Jay Ess said:

    i assume that is because the index has to be in order? so how would i get around that? other then keeping them in order?

    If you want order, then it is "ipairs" but what you say is correct, but not 100% accurate:

     

    Lua 5.3 spec for ipairs (t)

     

    Returns three values (an iterator function, the table t, and 0) so that the construction

         for i,v in ipairs(t) do body end
    will iterate over the key–value pairs (1,t[1]), (2,t[2]), ..., up to the first nil value.

     

    So if it wants item n but n is not a key ... iteration stops.

     

    Please login or register to see this code.

     

    If your keys are integers, but have "holes", or your keys are any other data type eg a date, you need 2 tables. One is holding the data. The other is a sorted table of the keys. You then do "ipairs" over the sorted table to retrieve the data.

    • 0
    Posted
    22 minutes ago, Jay Ess said:

    thank you and to get the index if i know the value?

    You have to search. Be aware they keys have to be unique - but values don't so you could have

    tb[2]=88

    tb[77]=88

    if you looked for the 88 value, should you return 2 or 77?

    If you have unique values too it's a common model to keep a "reverse lookup table" that maps value to keys.


     

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted

    your code gives me lua:241: attempt to index a nil value (global 't') copied and pasted exatly as you wrote it.

     

     

    also your function is within another function is that why i am getting the error?

    • 0
    Posted

    Sorry, the example assigns variable 'a' and then uses variable 't'

    Change 

    Please login or register to see this code.

    to

     

    Please login or register to see this code.

    • 0
  • Inquirer
  • Posted (edited)

    still not happy - lua:241: attempt to index a nil value (global 't')

     

    function create2wayTable()

       local r, tab,rev = {},{},{}

       function r.add(key, value) tab[key],rev[value]=value,key end

       function r.remove(key) local v = tab[key]; tab[key]=nil; if v~=nil then rev[v]=nil end end

       function r.lookupValue(key) return tab[key] end

       function r.lookupKey(value) return rev[value] end

       return res

    end

     

    t = create2wayTable()

    t.add(1,"First")

    print(t.lookupValue("First"))

    print(t.lookupKey(1))

     

     

    if i undertsnad your table correctly, this is what it would look like manually

    tb={{index},{key},{value}}

     

    Edited by Jay Ess
    • 0
    Posted

    Sorry again,

    It should return r at the end of the function

    Please login or register to see this code.

     

    • 0
  • Inquirer
  • Posted

    @jgab as usual you are amazing and a HUGE help thank you

    • 0
  • Inquirer
  • Posted

    Does it make a difference for run speed if I have a static table and populate the table instead of run add the lines to the table?

     

    Using your example how would I write this table without the add functions?

    • 0
    Posted (edited)

    The add function is to make sure that both tables are updated when adding and element.

    There is no relevant change in speed - we are talking about microseconds.

    However, we can expose the tables so that we can loop over them

     

    Please login or register to see this code.

     

    Edited by jgab
    • 0
  • Inquirer
  • Posted

    if i put the funtion create2waytable in a another sheet in the qa, why does it give me an error?

    • 0
    Posted

    What error do you get?

    • 0
  • Inquirer
  • Posted (edited)

    when i use this code for k,k in pairs(t.tab) do print(k,v) end

     

    and my add is like this

    local pTb = create2wayTable()

        local pIx = 0

        pTb.add(pIx,"Bereshit")

        pIx = pIx + 1

        pTb.add(pIx,"Noach")

     

    it only prints me out the value but not the key?

     

    re the error i have 5 sheets in the qa in one of the sheets i get this error

    attempt to call a nil value (global 'create2wayTable')

     

    however on another sheet it does work!

    Edited by Jay Ess

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...