lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


My question was silly, isn't it?

The 'i' in ipairs stands for integer right?

Thanks Ivan, the following works

student = {}
student.name = "Test"
student.age = 25

for i,v in pairs(student) do
    print(i,"=>",v)
end

test_arr = {4,5,6}
for i,v in pairs(test_arr) do
    print(i,"=>",v)
end

#############################
---------- Run ----------
name    =>    Test
age    =>    25
1    =>    4
2    =>    5
3    =>    6

Output completed (0 sec consumed) - Normal Termination

Cheers,
GG

On 12/8/06, Ivan Smirnov <i.s.smirnov@gmail.com> wrote:
Hello,

Iterator ipairs() goes over plain integer keys arrays - moreover, you won't like to use it for arrays with nil-gaps or any other anomalies.

As for your example, pairs() will work for both student and test_arr; ipairs() will work for test_arr. AFAIK, pairs() won't provide an ordered lookup for plain integer arrays.


On 12/7/06, Ganesh Gunasegaran < ganesh.gunasegaran@gmail.com> wrote:
Hi,

Does ipairs() work only with integer keys? if so why?

student = {}
student.name = "Test"
student.age = 25

for i,v in ipairs(student) do -- A
    print(i,"=>",v)
end

test_arr = {4,5,6}
for i,v in ipairs(test_arr) do -- B
    print(i,"=>",v)
end

#########################
---------- Run ----------
1    =>    4
2    =>    5
3    =>    6

Output completed (0 sec consumed) - Normal Termination

I expected that the for loop at A to print something like
"name"  =>    "Test"
"age"    =>    25


Cheers,
GG



--
Regards,
Ivan Smirnov

Department of Math and Stat Sciences
622 CAB
University of Alberta
Edmonton, AB, Canada
T6G 2G1

ismirnov@math.ualberta.ca, i.s.smirnov@gmail.com