Help me get the number of items in the table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help me get the number of items in the table

AndreyCh
Hi all

Help me get the number of items in the table

Lua 5.1

-- https://codereview.stackexchange.com/questions/14236/very-basic-chatbot

local messages = {
    [1] = "Hello stranger!",
    ["name"] = "I'm Bob, the owner of this little shop.",
    ["job"] = "I sell stuff.",
    ["hat"] = {
         [1] = "You like my hat?",
         ["yes"] = "It's nice I know.",
         ["no"] = "You ...!",
    },
}
local topic = messages;

print(">>",#topic); -- 1

The result is 1
But I need that result be 4


Thank you



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help me get the number of items in the table

szbnwer@gmail.com
you should write a function that walks through on a table with pairs()
and accumulates the steps
 into a variable, because ipairs() and # only works on the array part,
so 1,2,3... indexes in a line from 1, but if there are nil-s before
another number keys, then thats something you should not relay on with
#, like {1,2,nil,4,nil,nil,nil,8} will give 8 for lenght because of
binary search, so that is searching exponentially... my implementation
works for strings and tables as well, use it as you like it, you can
even reduce the string part:

len=function(stuff)
  local typ=type(stuff)
  if typ=='string' then return #stuff
  elseif typ=='table' then
    local l=0
    for _ in pairs(stuff) do
      l=l+1 end
    return l end end

have fun with lua, it's the best language out there :D

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help me get the number of items in the table

szbnwer@gmail.com
a good roadmap to learn lua:

https://www.lua.org/pil/ - basic knowledge
https://www.lua.org/manual/5.1/manual.html#7 - differences between 5.0
and 5.1; and the whole reference manual itself, you can let the c
parts to the end, so like half of the manual (chapter 3 and 4)
https://www.lua.org/ - in general, because it's a nice set
http://lua-users.org/ - here is a great collection of kinda much
anything around lua that you can find in the wild...

and of course this mailing list as this made my basic knowledge sharp :D

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help me get the number of items in the table

Luiz Henrique de Figueiredo
In reply to this post by AndreyCh
> Help me get the number of items in the table

Why do you need this number? What problem are you really try to solve?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help me get the number of items in the table

Rena
In reply to this post by AndreyCh
On Jul 29, 2017 7:37 AM, "AndreyCh" <[hidden email]> wrote:
Hi all

Help me get the number of items in the table

Lua 5.1

-- https://codereview.stackexchange.com/questions/14236/very-basic-chatbot

local messages = {
    [1] = "Hello stranger!",
    ["name"] = "I'm Bob, the owner of this little shop.",
    ["job"] = "I sell stuff.",
    ["hat"] = {
         [1] = "You like my hat?",
         ["yes"] = "It's nice I know.",
         ["no"] = "You ...!",
    },
}
local topic = messages;

print(">>",#topic); -- 1

The result is 1
But I need that result be 4


Thank you



The result is 1 because # is meant to be used with sequences. It gives you some n for which key n+1 is absent.

To count the number of items in a non-sequential table, use pairs() to loop over them and count.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help me get the number of items in the table

AndreyCh
On Sun, 30 Jul 2017 11:44:15 -0400
Rena <[hidden email]> wrote:

> On Jul 29, 2017 7:37 AM, "AndreyCh" <[hidden email]> wrote:
>
> Hi all
>
> Help me get the number of items in the table
>
> Lua 5.1
>
> -- https://codereview.stackexchange.com/questions/14236/very-basic-chatbot
>
> local messages = {
>     [1] = "Hello stranger!",
>     ["name"] = "I'm Bob, the owner of this little shop.",
>     ["job"] = "I sell stuff.",
>     ["hat"] = {
>          [1] = "You like my hat?",
>          ["yes"] = "It's nice I know.",
>          ["no"] = "You ...!",
>     },
> }
> local topic = messages;
>
> print(">>",#topic); -- 1
>
> The result is 1
> But I need that result be 4
>
>
> Thank you
>
>
>
> The result is 1 because # is meant to be used with sequences. It gives you
> some n for which key n+1 is absent.
>
> To count the number of items in a non-sequential table, use pairs() to loop
> over them and count.


Thank you.  I already helped :-)


-- by szbnwer
len=function(stuff)
  local typ=type(stuff)
  if typ=='string' then return #stuff
  elseif typ=='table' then
    local l=0
    for _ in pairs(stuff) do
      l=l+1 end
    return l end
end


--
AndreyCh <[hidden email]>

Loading...