Difference between revisions of "Tuple"
From OpenEUO
m (Created page with "== Definition == A tuple is an ordered sequence of elements. Functions in lua can return more than one value at a time. E.g., local b = function(...) local a = {...} ...") |
m (→Definition) |
||
Line 6: | Line 6: | ||
local a = {...} | local a = {...} | ||
local c = select('#',...) | local c = select('#',...) | ||
− | return unpack(a) | + | return c, unpack(a) |
end | end | ||
+ | local d,e,f = b('A','B','C') | ||
+ | print(tostring(d)..' '..e..' '..f) | ||
+ | --> '2 A B' | ||
+ | |||
+ | the example function b takes a variable number of arguments and returns a variable number of results. When called as shown, the resulting tuple is (2,'A','B','C'). Elements of a tuple not assigned to a variable are simply discarded (so 'C', is dropped in the example case). | ||
== See Also == | == See Also == |
Latest revision as of 14:35, 1 November 2010
Definition
A tuple is an ordered sequence of elements. Functions in lua can return more than one value at a time. E.g.,
local b = function(...) local a = {...} local c = select('#',...) return c, unpack(a) end local d,e,f = b('A','B','C') print(tostring(d)..' '..e..' '..f)
--> '2 A B'
the example function b takes a variable number of arguments and returns a variable number of results. When called as shown, the resulting tuple is (2,'A','B','C'). Elements of a tuple not assigned to a variable are simply discarded (so 'C', is dropped in the example case).