9 posts
Posted 04 October 2014 - 10:16 PM
How would you go about making tables containing tables. For example:
countries={
france={"Paris", "Nantes", "Bourges"},
england={"london, "Big Ben"},
germany={"berlin", …ect}
}
Can you also still have it so I could search the countries array by user input? Example:
io.write("input: ")
input=io.read()
i=1
while countries[i] do
if countries[i]==input then
print("found")
break
end
i=i+1
end
Edited on 04 October 2014 - 08:19 PM
1080 posts
Location
In the Matrix
Posted 04 October 2014 - 10:27 PM
With a table the way you have it such as
local countries = {france = {"lala","wut?"}, germany = {"one","two,}}
You need to use this to find the names of the countries, i.e. france and germany.
for country,cities in pairs(countries) do --#Since your table uses the country name as a key and not a value you have to do this to get the name of the key
if country == input then
print("Found!")
break
end
end
9 posts
Posted 05 October 2014 - 04:50 AM
Thanks! I'm not sure I understand how it works so I guess Ill have to look into iterators. (I think thats whats going on here?) How could you go about printing the the strings inside the "input" table if its found it.
Thanks again for the help. I'm trying to learn lua in my spare time and it can be frustrating lol.
1852 posts
Location
Sweden
Posted 05 October 2014 - 11:24 AM
Thanks! I'm not sure I understand how it works so I guess Ill have to look into iterators. (I think thats whats going on here?) How could you go about printing the the strings inside the "input" table if its found it.
Thanks again for the help. I'm trying to learn lua in my spare time and it can be frustrating lol.
Here's a simple version of Dragon53535's code, this also prints the cities
if countries[input:lower()] then
print( "Found!" )
for _, city in pairs( countries[input:lower()] ) do
print( city )
end
end
As you can see, I'm using
input:lower() and this basically converts the input from example "HeLLo WOrLD!" to "hello world!"
When using the colon syntax I can do this
input:lower()
instead of this
string.lower( input )
Edited on 05 October 2014 - 09:24 AM
9 posts
Posted 05 October 2014 - 11:00 PM
Thanks! This is exactly what I was looking for! Especially the tip for input:lower(). You guys are fantastic!