1

I was trying for in loop to traverse through key value pairs :

a = {1, 2, 7, val4="val 4", val5="val 5", 9, "ten"}

print (a['val4'])
print (a[2])

print ("--- for in loop ---")
for k ,v in ipairs(a) do
  print (k,v)
end 

output:

val 4
2
--- for in loop ---
1   1
2   2
3   7
4   9
5   ten

Can someone tell me why val4 and val5 was not retrived, what can I do to get them

hers the test in rpl.it: http://repl.it/Wzw/2

1 Answer 1

3

ipairs is used for sequences, it only iterates through integer keys from 1, 2, etc. But your table has non-integer keys like "val4" and "val5". Use pairs instead:

for k ,v in pairs(a) do
  print (k,v)
end 
Sign up to request clarification or add additional context in comments.

1 Comment

That was ture, thakns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.