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
.
Summary of Contents for Camera
Page 1: ......
Page 2: ...The Canon Camera Hackers Manual ...
Page 3: ......
Page 4: ...Berthold Daum The Canon Camera Hackers Manual Teach Your Camera New Tricks ...
Page 19: ...10 CH APTER 2 Cameras and Operating Systems ...
Page 25: ...16 CH APTER 3 ...
Page 85: ...76 CH APTER 4 Teach Your Camera New Tricks ...
Page 213: ...204 CH APTER 6 ...
Page 253: ...244 AP PENDIX ...