Module:Pn
Appearance
The module returns one value from its list of unnamed parameters.
The named parameter |idx=
is the index of the parameter that is to be returned.
Negative indices count backward from the end of the list.
A wrapper template may be used to simplify usage.
For accessing both named and unnamed parameters, see {{#invoke:params|value_of}}.
Examples
{{#invoke:Pn |getVal | idx=1}}
→ Lua error at line 13: No values supplied.{{#invoke:Pn |getVal | idx= | a | b | c | d | e | f }}
→{{#invoke:Pn |getVal | idx=0 | a | b | c | d | e | f }}
→{{#invoke:Pn |getVal | idx=1 | a | b | c | d | e | f }}
→{{#invoke:Pn |getVal | idx=2 | a | b | c | d | e | f }}
→{{#invoke:Pn |getVal | idx=-1 | a | b | c | d | e | f }}
→{{#invoke:Pn |getVal | idx=99 | a | b | c | d | e | f }}
→
Using a wrapper template, {{P-1}}:
{{p-1 | a | b | c | d | e | f }}
→{{wdib|ps=1|P8011|qid=Q84055514}}
→{{wdib|ps=1|P8011|qid=Q84055514|list=p-1}}
→
See also
--[[
Module that returns one value from a list of unnamed parameters
Named parameter idx is the index of the parameter that is to be returned
Negatives indices count backward from the end of the list
==]]
local p = {}
p.getVal = function(frame)
local args= frame.args
if not args[1] then
args = frame:getParent().args
if not args[1] then return error("No values supplied") end
end
local idx = tonumber(args.idx) or 1
if idx < 1 then idx = #args + idx + 1 end
if idx > #args then idx = #args end
return args[idx], #args
end
return p