Jump to content




Arrays/tables

lua

  • You cannot reply to this topic
4 replies to this topic

#1 Micheal Pearce

  • Members
  • 87 posts
  • LocationUSA

Posted 17 July 2013 - 11:51 AM

so i've been trying to use table lately and i don't understand why this is not working i swear it work sometime ago but now it doesn't

ex = {
   a = 1,
   A = 10,
}
print(ex[a])

and this doesn't work and i know i could do ex.a and it would work but need it so it can take a user inputted letter so like

ex = {
a = 1,
A = 10,
b = 2,
B = 20,
c = 3,
C = 30,
}
input = read()
print(ex[input])

and if it would work the way i think it should it should print out 10 if the use inputted A, but this doesn't it doesn't return anything but a blank line another thing is when i try to get how many values are in the table/array like print(#ex) would return 0

Thank you for reading this and thanks for you comments

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 17 July 2013 - 12:10 PM

 Yuri, on 17 July 2013 - 11:51 AM, said:

so i've been trying to use table lately and i don't understand why this is not working i swear it work sometime ago but now it doesn't

ex = {
   a = 1,
   A = 10,
}
print(ex[a])

and this doesn't work and i know i could do ex.a and it would work but need it so it can take a user inputted letter so like
You need to add quotes there:
print(ex["a"])
wich is the same as:
print(ex.a)

 Yuri, on 17 July 2013 - 11:51 AM, said:

another thing is when i try to get how many values are in the table/array like print(#ex) would return 0
Using the lenght operator only returns

Quote

the largest positive numerical index of the given table, or zero if the table has no positive numerical indices.
To get the size of a table with string (or any other type of) keys, you can use a function like this:
local function getTableSize(t)
  local size = 0
  for k,v in pairs(t) do
	size = size + 1
  end
  return size
end


#3 Micheal Pearce

  • Members
  • 87 posts
  • LocationUSA

Posted 17 July 2013 - 12:56 PM

 MysticT, on 17 July 2013 - 12:10 PM, said:

You need to add quotes there:
print(ex["a"])

so how would i be able to get use input and use it to get one of the values in the ex table?

#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 17 July 2013 - 01:41 PM

 Yuri, on 17 July 2013 - 12:56 PM, said:

 MysticT, on 17 July 2013 - 12:10 PM, said:

You need to add quotes there:
print(ex["a"])

so how would i be able to get use input and use it to get one of the values in the ex table?

local ex = {
  stuff = "hello";
  ["tables are versatile"] = "sup";
}

local input = read()
if ex[input] then --#You are inputting the /value/ of input, not trying to get a field called "input" in the ex table
  print(ex[input])
end

So if I input "stuff" to the prompt, the program would print out "hello".

#5 Micheal Pearce

  • Members
  • 87 posts
  • LocationUSA

Posted 17 July 2013 - 02:45 PM

Thanks guys!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users