Select: Difference between revisions

From GiderosMobile
Created page with "__NOTOC__ '''Available since:''' Gideros 2011.6<br/> '''Class:''' (global)<br/> === Description === Returns items in a list. <source lang="lua"> (any) = select(index,...)..."
 
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
=== Description ===
=== Description ===
Returns items in a list.
Returns items in a list.
<source lang="lua">
<syntaxhighlight lang="lua">
(any) = select(index,...)
(any) = select(index,...)
</source>
</syntaxhighlight>


If index is a number, returns all items in the list from that number onwards. Otherwise index must be the string '''"#"''', in which case it returns the number of items in the list.
If index is a number, returns all items in the list from that number onwards. Otherwise index must be the string '''"#"''', in which case it returns the number of items in the list.
Line 16: Line 16:


=== Examples ===
=== Examples ===
<source lang="lua">
<syntaxhighlight lang="lua">
function f(...)
function f(...)
print (select ("#", ...))  --> 4
print (select ("#", ...))  --> 4
Line 23: Line 23:


f("b", "r", 32, "z")
f("b", "r", 32, "z")
</source>
</syntaxhighlight>


'''Select a random weapon'''
'''Select a random weapon'''
<source lang="lua">
<syntaxhighlight lang="lua">
local random = math.random
local random = math.random


function choose(...)
function choose(...)
local index = random(select("#", ...))
local index = random(select("#", ...))
local result = select(index, ...) -- result stores only the value at the index (omitting the other values)
local result = select(index, ...) -- store only value at index (omitting the other values)
-- local result = select(random(select("#",...)), ...) -- same as above but in one line
-- local result = select(random(select("#",...)), ...) -- same as above but in one line
return result
return result
Line 38: Line 38:
local weapon = choose("pistol", "auto", "falcon", "rpg", "test")
local weapon = choose("pistol", "auto", "falcon", "rpg", "test")
print("random weapon: "..weapon) -- eg. "random weapon: falcon"
print("random weapon: "..weapon) -- eg. "random weapon: falcon"
</source>
</syntaxhighlight>
 
=== See also ===
'''https://luau.org/library#global-functions'''<br/>


{{(global)}}
{{(global)}}

Latest revision as of 10:25, 21 July 2025

Available since: Gideros 2011.6
Class: (global)

Description

Returns items in a list.

(any) = select(index,...)

If index is a number, returns all items in the list from that number onwards. Otherwise index must be the string "#", in which case it returns the number of items in the list.

Parameters

index: (number or "#") number, all items in the list from that number onwards. "#", number of items in the list
...: (any) the list to process

Examples

function f(...)
	print (select ("#", ...))  --> 4
	print (select (2, ...))    --> r  32  z
end

f("b", "r", 32, "z")

Select a random weapon

local random = math.random

function choose(...)
	local index = random(select("#", ...))
	local result = select(index, ...) -- store only value at index (omitting the other values)
--	local result = select(random(select("#",...)), ...) -- same as above but in one line
	return result
end

local weapon = choose("pistol", "auto", "falcon", "rpg", "test")
print("random weapon: "..weapon) -- eg. "random weapon: falcon"

See also

https://luau.org/library#global-functions