
2600AS-901-01 Rev. B / September 2008
Return to
19-5
Series 2600A System SourceMeter® Instruments Reference Manual
Section 19: Remote Commands
Tables/arrays
TSL makes extensive use of the data type “table,” which is essentially a very flexible array-like
data type.
Define a table:
atable = {1, 2, 3, 4}
Print it:
-- Tables are indexed starting at one, NOT zero.
i = 1
-- atable[index] is true if there is an element at that index; nil is returned
-- otherwise. 0 does NOT evaluate to false - only nil does.
while atable[i] do
-- Index into table using a number.
print(atable[i])
i = i + 1
end
Output of code above:
1
2
3
4
Tables can be indexed using element names instead of numeric indices. Since functions are first-
class variables, tables can be used to create "pseudo-classes." Classes are often used in object-
oriented programming.
Below is a table used to create a circle pseudo-class. It has 3 elements:
clr
: A string containing the color of the circle.
diam
: A number containing the diameter of the circle.
setdiam
: A function, or method, used to change the diameter.
circle = {clr = "red", diam = 1, setdiam = function(d)
circle["diam"] = d end}
-- Index using a string; print the clr property.
print(circle["clr"])
-- Index using a string; print the diam property.
print(circle["diam"])
-- Change the diam element by calling setdiam method.
circle["setdiam"](2)
-- circle["diam"] is the same as circle.diam; simpler syntax.
print(circle.diam)
-- Change the diameter of the circle again.
circle.setdiam(3)
-- Print diam property again using simple syntax.
print(circle.diam)
Output of code above:
red
1
2
3