EasyManua.ls Logo

Canon Camera - Page 106

Canon Camera
257 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
5.4 Lua primer 97
for i = 1,10,3 do
print(i)
end
would print the values 1, 4, 7, 10.
A second form of the for statement—called the generic for state-
ment—allows iteration over a series of values, such as the elements of a
table. Using the built-in function ipairs(), we can write:
disp_table = {'info', 'no_info', 'off', 'electronic_viewfinder'}
for i,v in ipairs(disp_table) do
print(i,v)
end
This results in the output:
1 info
2 no_info
3 off
4 electronic_viewfinder
The same is possible for dictionary tables that are accessible via keywords.
Instead of ipairs(), the function pairs() is used in this case:
t = {info = 0, no_info = 1, off = 2, electronic_viewfinder = 3}
for key,value in pairs(t) do
print(key,value)
end
The result of this script would be:
electronic_viewfinder 3
off 2
info 0
no_info 1
Note that in this case the order in which the values are obtained cannot be
predicted.
In addition to the various flavors of the for statement, Lua also features
a while statement and a repeat statement. The latter replaces the do...
until construct found in uBasic.

Table of Contents

Related product manuals