To iterate over all the key-value pairs in a table you can use pairs
:
for k, v in pairs(arr) do print(k, v[1], v[2], v[3]) end
outputs:
pears 2 p green apples 0 a red oranges 1 o orange
Edit: Note that Lua doesn’t guarantee any iteration order for the associative part of the table. If you want to access the items in a specific order, retrieve the keys from arr
and sort it. Then access arr
through the sorted keys:
local ordered_keys = {} for k in pairs(arr) do table.insert(ordered_keys, k) end table.sort(ordered_keys) for i = 1, #ordered_keys do local k, v = ordered_keys[i], arr[ ordered_keys[i] ] print(k, v[1], v[2], v[3]) end
outputs:
apples a red 5 oranges o orange 12 pears p green 7
Related Posts:
- Correct way of looping through C++ arrays
- How to remove a lua table entry by its key?
- Bubble sort algorithm in MIPS
- For-each over an array in JavaScript
- Loop through an array in JavaScript
- How to split a string into an array in Bash?
- Loop through an array in JavaScript
- Converting array to list in Java
- What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
- Java Error “Exception in thread “main” java.util.InputMismatchException” On an Array program
- How to print elements in a vector c++
- TypeScript Objects as Dictionary types as in C#
- Data type not understood while creating a NumPy array
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
- Are vectors passed to functions by value or by reference in C++
- Loop through an array php
- How do I declare a 2d array in C++ using new?
- “error: assignment to expression with array type error” when I assign a struct field (C)
- Does JavaScript have a method like “range()” to generate a range within the supplied bounds?
- Getting “conflicting types for function” in C, why?
- Javascript Uncaught TypeError: Cannot read property ‘0’ of undefined
- Does JavaScript have a method like “range()” to generate a range within the supplied bounds?
- Form submission: PHP S_SESSION statements within a foreach loop
- How to split a string into an array in Bash?
- Working with a List of Lists in Java
- Array Size (Length) in C#
- Getting “conflicting types for function” in C, why?
- How to convert jsonString to JSONObject in Java
- Array of arrays (Python/NumPy)
- Why is using “for…in” for array iteration a bad idea?
- what does “>>>” mean in java?
- How to convert List
to int[] in Java? - Calculating Standard Deviation & Variance in C++
- Javascript Uncaught TypeError: Cannot read property ‘0’ of undefined
- How to convert int[] into List
in Java? - What is a list in Bash?
- What does ** do in C language?
- When will the worst case of Merge Sort occur?
- In Java, how to append a string more efficiently?
- How do I create an array of strings in C?
- Fill array with random numbers within a specified range (C++)
- ow to create a histogram in java
- what does .space do in mips?
- How do I remove an array item in TypeScript?
- What does << mean in Ruby?
- How to create an array of strings in C?
- Initialization from incompatible pointer type warning when assigning to a pointer
- Error: unsupported use of matrix or array for column indexing
- extended initializer lists only available with
- Bank Account Java Program
- Using multiple variables in a for loop in Python
- Weighted random selection from array
- Badly placed ()’s error with the following shell script
- type any? has no subscript members
- Multidimensional Vectors in C++
- Java ArrayList for integers
- java.lang.ArrayIndexOutOfBoundsException: 4 Error
- IndexError: index 10 is out of bounds for axis 0 with size 10
- Why does Lua have no “continue” statement?
- How to sort an array in Bash
- How can I find the number of elements in an array?
- Array type char[] is not assignable
- Create a two dimensional string array anArray[2][2]
- Two-dimensional array in Swift
- Parameter name omitted error?
- What is the “right” way to iterate through an array in Ruby?
- C char array initialization
- How to sort in-place using the merge sort algorithm?
- PHP array printing using a loop
- VBA array sort function?
- How to declare and use 1D and 2D byte arrays in Verilog?
- Difference between char *argv[] and char **argv for the second argument to main()
- Convert ArrayList
to java.util.List [closed] - Dynamic vs static array in c
- Array to Hash Ruby
- Printing from a declared array in MIPS
- Print array elements on separate lines in Bash?
- Java says this method has a constructor name
- Create an empty object in JavaScript with {} or new Object()?
- How do I append to a table in Lua
- GCC: Array type has incomplete element type
- What is the best way to delete a value from an array in Perl?
- Checking if a key exists in a JavaScript object?
- variably modified array at file scope in C
- Numpy matrix to array
- Using the Pythagorean theorem with Java
- How do I create an empty array/matrix in NumPy?
- Converting array to list in Java
- How do you make an array of structs in C?
- How to filter an array/object by checking multiple values
- How to fix array index out of bounds error?
- Passing a 2D array to a C++ function
- Returning an array using C
- How to add an object to an array
- Merging two arrays in .NET
- Finding the average of an array using JS
- C# syntax to initialize custom class/objects through constructor params in array?
- How to convert array to SimpleXML
- Return array from function
- How to use a Do-while loop that continuously prompts a user?