Module:For loop
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 853,000 pages, or roughly 1% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
This module implements Template:For loop (edit | talk | history | links | watch | logs). Please see the template page for documentation.
p = {}
local args
local function callTemplate(template, targs)
return mw.getCurrentFrame():expandTemplate{title = template, args = targs}
end
local function getArgNums(prefix, suffix)
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local nums = {}
for k, v in pairs(args) do
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)' .. suffix .. '$')
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
local function getConstants()
local constantArgNums = getArgNums('pc', 'n')
local constantArgs = {}
for _, v in ipairs(constantArgNums) do
local keyArgName = 'pc' .. tostring(v) .. 'n'
local valArgName = 'pc' .. tostring(v) .. 'v'
constantArgs[args[keyArgName]] = args[valArgName]
end
return constantArgs
end
local function getVariableVals()
local variableVals = {}
for i, v in ipairs(args) do
if i ~= 1 then
variableVals[i - 1] = v
end
end
return variableVals
end
local function _main()
local template = args['call'] or 'void'
local variableParam = args.pv
local variableValPrefix = args.prefix
local variableValPostfix = args.postfix
local sep = args[1] or ''
local constantArgs = getConstants()
local variableVals = getVariableVals()
result = ''
for i, v in ipairs(variableVals) do
v = mw.text.trim(v) -- trim whitespace
local targs = constantArgs
targs[variableParam or 1] = (variableValPrefix or '') .. tostring(v) .. (variableValPostfix or '')
result = result .. tostring(callTemplate(template, targs))
if variableVals[i + 1] then
result = result .. sep
end
end
return result
end
function p.main(frame)
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
if frame == mw.getCurrentFrame() then
args = frame:getParent().args
for k, v in pairs(frame.args) do
args = frame.args
break
end
else
args = frame
end
return _main()
end
return p