I'm attempting to iterate through an array, like the following:
1 | local array = { |
2 | [ 'hi' ] = "Hello" ; |
3 | [ 'test' ] = "Greetings" ; |
4 | } |
and print out the key and value like:
hi = Hello
My current code is:
1 | for i,v in ipairs (array) do |
2 | print (i.. " = " ..v) |
3 | end |
ipairs
specifically only iterates through integer numerical indices. What you want to use is pairs
:
1 | for i,v in pairs (array) do |
2 | print (i.. " = " ..v) |
3 | 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.
1 | local array = { |
2 | Hi = "Hello" , -- hi is just a codename for Hello like print(array.Hi) would print out Hello |
3 | Bye = "Good-Bye" |
4 | } |
5 |
6 | for i,v in pairs (array) do |
7 | print (v) |
8 | end |