Select: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Thisismypassphrase
(Created page with "{{ApiReference |name=select |shortdesc=Selects from the given parameters. |index||Index to return parameters from, or '#' to return number of parameters |...||parameters||...")
 
(added an example that uses varargs, alluding to the intended use of select)
 
(3 intermediate revisions by 2 users not shown)
Line 10: Line 10:
== Examples ==
== Examples ==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
a, b, c = select(4,1,2,3,4,5,6) -- returns 4, 5, 6
a, b, c = select(4,1,2,3,4,5,6) -- returns 4, 5, 6
print(a)                       -- prints 4
print(a)                         -- prints 4
print(b)                       -- prints 5
print(b)                         -- prints 5
print(c)                       -- prints 6
print(c)                         -- prints 6


print(select('#',1,2,3,nil,nil)) -- prints 5
print(select('#',1,2,3,nil,nil)) -- prints 5
print(select('#')) -- prints 0
print(select('#'))               -- prints 0
 
function print_all(...)
if select("#",...)>0 then
  print(select(1,...),cursor())
  print_all(select(2,...))
end
end
 
print_all(1,2,3,4,6,8)          --prints 1 2 3 4 6 8
</syntaxhighlight>
</syntaxhighlight>


== See also ==
== See also ==
* [[Pack|<code>pack()</code>]]
* <code>[[Pack|pack()]]</code>
* <code>[[Unpack|unpack()]]</code>
[[Category:Reference]]
[[Category:Reference]]
[[Category:Api]]
[[Category:API]]
[[Category:Lua]]
[[Category:Lua]]

Latest revision as of 06:18, 26 March 2021

select( index, ... )
Selects from the given parameters.
index
Index to return parameters from, or '#' to return number of parameters

...
parameters

If index is a number, returns all parameters starting at index index. If index is '#', returns the number of parameters passed.

Examples

a, b, c = select(4,1,2,3,4,5,6)  -- returns 4, 5, 6
print(a)                         -- prints 4
print(b)                         -- prints 5
print(c)                         -- prints 6

print(select('#',1,2,3,nil,nil)) -- prints 5
print(select('#'))               -- prints 0

function print_all(...)
 if select("#",...)>0 then
  print(select(1,...),cursor())
  print_all(select(2,...))
 end
end

print_all(1,2,3,4,6,8)          --prints 1 2 3 4 6 8

 See also