2

I have the following array

local Array = {}
Array[1] = {"Value_A", "Value_B", "Value_C", "Value_D"}
Array[2] = {"Value_E", "Value_F", "Value_G", "Value_H"}
Array[3] = {"Value_I", "Value_J", "Value_K", "Value_L"}

and I know I can print the values of every array item at once with

for k, v in ipairs(Array) do
  print(k, v[1], v[2], v[3], v[4])
end

but I'd like to print the values of specific array items.

How can I do that?

10
  • 1
    for k, v in pairs(Array[2]) do print(k, v) end Commented Dec 1, 2017 at 19:34
  • 1
    Mike's code does what you requested. Commented Dec 1, 2017 at 19:55
  • 1
    hard to understand you, just print what you want Commented Dec 1, 2017 at 20:27
  • 1
    print(Array[2][3]) to print separately Commented Dec 1, 2017 at 21:22
  • 2
    well if you are unable to tell people what you want you will never be able to tell a computer what you want it to do.... edit your question, give the input and the desired output. Commented Dec 1, 2017 at 21:23

1 Answer 1

2

From reading your post and comments it seems to me you want to print every value separately instead of it all being on one line.

To do this you would need another for loop to iterate through all the values.

for k, v in ipairs(Array) do
          print(k)
          for i=1, #v do print(v[i]) end
          print() // This will just print a new line
end

Output:

1
Value_A
Value_B
Value_C
Value_D

2
Value_E
Value_F
Value_G
Value_H

3
Value_I
Value_J
Value_K
Value_L
Sign up to request clarification or add additional context in comments.

Comments

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.