I'm attempting to iterate through an array, like the following:
local array ={
['hi'] = "Hello";
['test'] = "Greetings";
}
and print out the key and value like:
hi = Hello
My current code is:
for i,v in ipairs(array) do
print(i.." = "..v)
end
I'm attempting to iterate through an array, like the following:
local array ={
['hi'] = "Hello";
['test'] = "Greetings";
}
and print out the key and value like:
hi = Hello
My current code is:
for i,v in ipairs(array) do
print(i.." = "..v)
end
ipairs specifically only iterates through integer numerical indices. What you want to use is pairs:
for i,v in pairs(array)do
print(i.." = "..v)
end
well, what I see happening is you are putting tables in arrays and the tables are something, idk your thing is hard to read. What I would do though is.
local array = {
Hi = "Hello", -- hi is just a codename for Hello like print(array.Hi) would print out Hello
Bye = "Good-Bye"
}
for i,v in pairs(array) do
print(v)
end
This discussion is preserved as it was. New answers, comments, and votes are closed.
5 comments
This is the answer you're looking for, why did someone vote down? I'm voting up, he doesn't deserve to lose rep for this, it works!
I agree with Undeniable, though unfortunately, I do not have enough reputation to up-vote. Just know that if I could, I most certainly would.
It's worth pointing out that 'i' for 'index' isn't a good variable name in this situation, because you're not using a list.
Index doesn't necessarily mean integer. An index can be anything. It could be the table itself. t={} t[t]=t print(t==t[t[t[t]][t]])
Who downvoted me, we're about to fight.