Search code examples
lua

We giving a task for Lua table but it is not working as expectable


Our task is create a table, and read values to the table using a loop. Print the values after the process is complete. - Create a table. - Read the number of values to be read to the table. - Read the values to the table using a loop. - Print the values in the table using another loop. for this we had written code as

local table = {}

for value in ipairs(table) do 
   io.read()
end

for value in ipairs(table) do
  print(value)
end

not sure where we went wrong please help us. Our exception is

Input (stdin)
3
11
22
abc

Your Output (stdout)
~ no output ~

Expected Output
11
22
abc

Correct Code is

local table1 = {} 
local x = io.read() 

for line in io.lines() do 
table.insert(table1, line) 
end 

for K, value in ipairs(table1) do
 print(value) 
end 

Solution

  • Let's walk through this step-by-step.

    1. Create a table.

    Though the syntax is correct, table is a reserved pre-defined global name in Lua, and thus cannot should not be declared a variable name to avoid future issues. Instead, you'll need to want to use a different name. If you're insistent on using the word table, you'll have to distinguish it from the function global table. The easiest way to do this is change it to Table, as Lua is a case-sensitive language. Therefore, your table creation should look something like:

    local Table = {}
    
    1. Read values to the table using a loop.

    Though Table is now established as a table, your for loop is only iterating through an empty table. It seems your goal is to iterate through the io.read() instead. But io.read() is probably not what you want here, though you can utilize a repeat loop if you wish to use io.read() via table.insert. However, repeat requires a condition that must be met for it to terminate, such as the length of the table reaching a certain amount (in your example, it would be until (#Table == 4)). Since this is a task you are given, I will not provide an example, but allow you to research this method and use it to your advantage.

    1. Print the values after the process is complete.

    You are on the right track with your printing loop. However, it must be noted that iterating through a table always returns two results, an index and a value. In your code, you would only return the index number, so your output would simply return:

    1
    2
    3
    4
    

    If you are wanting the actual values, you'll need a placeholder for the index. Oftentimes, the placeholder for an unneeded variable in Lua is the underscore (_). Modify your for loop to account for the index, and you should be set.

    Try modifying your code with the suggestions I've given and see if you can figure out how to achieve your end result.

    Edited:

    Thanks, Piglet, for corrections on the insight! I'd forgotten table itself wasn't a function, and wasn't reserved, but still bad form to use it as a variable name whether local or global. At least, it's how I was taught, but your comment is correct!