
Section 6: Instrument programming
Model 2657A High Power System SourceMeter® Instrument Reference Manual
6-24
2657A-901-01 Rev. B/December 2012
Example: Break with infinite loop
a, b = 0, 1
while true do
print(a, b)
a, b = b, a + b
if a > 500 then
break
end
end
This example uses a
break
statement
that causes the while loop to exit if the
value of
a
becomes greater than 500.
Output:
0.00 1.00
1.00 1.00
1.00 2.00
2.00 3.00
3.00 5.00
5.00 8.00
8.00 1.01
1.01 2.01
2.01 3.01
3.01 5.01
5.01 8.01
8.01 1.02
1.02 2.02
2.02 3.02
3.02 6.02
Tables and arrays
Lua makes extensive use of the data type table, which is a flexible array-like data type. Table indices
start with 1. Tables can be indexed not only with numbers, but with any value except
nil
. Tables can
be heterogeneous, which means that they can contain values of all types except
nil
.
Tables are the sole data structuring mechanism in Lua. They may be used to represent ordinary
arrays, symbol tables, sets, records, graphs, trees, and so on. To represent records, Lua uses the
field
name
as an index. The language supports this representation by providing
a.name
as an easier
way to express
a["name"]
.
Example: Loop array
atable = {1, 2, 3, 4}
i = 1
while atable[i] do
print(atable[i])
i = i + 1
end
Defines a table with four numeric
elements.
Loops through the array and prints
each element.
The Boolean value of
atable[index]
evaluates to
true
if
there is an element at that index. If
there is no element at that index,
nil
is returned (
nil
is considered to be
false
).
Output:
1.00
2.00
3.00
4.00
Summary of Contents for 2657A
Page 3: ......