Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 

















Module:For loop






Afrikaans
Аԥсшәа
العربية
Авар
Azərbaycanca
تۆرکجه
Basa Bali

Banjar
 / Bân-lâm-gú
Башҡортса
Беларуская (тарашкевіца)

Bikol Central
Bosanski
Буряад
Català
Cebuano
Corsu
Cymraeg
Dolnoserbski
Español
Esperanto
Euskara
فارسی
Français
Galego
/Hak-kâ-ngî

Հայերեն
Hornjoserbsce
Ilokano
Bahasa Indonesia
Íslenska
Jawa


Kurdî
Ladin
Latviešu
Lietuvių
Magyar
Македонски

مصرى
Bahasa Melayu
 / Mìng-dĕ̤ng-nḡ
Nederlands


Oʻzbekcha / ўзбекча
Polski
Português
Qaraqalpaqsha
Русиньскый
Русский
Саха тыла
Scots

Simple English
سنڌي
Slovenščina
کوردی
Српски / srpski
Suomi
Tagalog
ி
 

Тоҷикӣ

Türkçe
Українська
اردو
Tiếng Vit
Walon


 

Edit links
 









Module
Talk
 

















Read
View source
View history
 








Tools
   


Actions  



Read
View source
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



Wikimedia Commons
Meta-Wiki
Wikimedia Outreach
Wikispecies
Wikimania
Wikiversity
 
















Appearance
   

 





Permanently protected module

From Wikipedia, the free encyclopedia
 


This module implements Template:For loop (edit | talk | history | links | watch | logs). Please see the template page for documentation.

-- This module implements {{for loop}}.

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}

function p.main(frame)
 local args = getArgs(frame, {
  trim = false,
  removeBlanks = false
 })
 return p._main(args)
end

function p._main(args)
 local template = args['call'] or 'void'
 local calltemplates = yesno(args.substall or "", true) or not mw.isSubsting()
 local variableParam = args.pv
 variableParam = tonumber(variableParam) or variableParam or 1 -- fix for positional parameters
 local variableValPrefix = args.prefix or ''
 local variableValPostfix = args.postfix or ''
 local sep = args[1] or ''
 local constantArgs = p.getConstants(args)
 local variableVals = p.getVariableVals(args)

 local result = ''
 local addSeparator = false;
 for _, v in ipairs(variableVals) do
  v = mw.text.trim(v)
  if #v > 0 or not yesno(args.skipBlanks) then
   if addSeparator then
    result = result .. sep
   end
   addSeparator = true;
   local targs = constantArgs
   targs[variableParam] = variableValPrefix .. v .. variableValPostfix
   if calltemplates then
    local output = p.callTemplate(template, targs)
    if #mw.text.trim(output) == 0 then
     addSeparator = false
    end
    result = result .. output
   else
    local makeTemplate = require('Module:Template invocation').invocation
    result = result .. makeTemplate(template, targs)
   end
  end
 end
 return result
end

function p.getConstants(args)
 local constantArgNums = p.getArgNums(args, 'pc', 'n')
 local constantArgs = {}
 for _, num in ipairs(constantArgNums) do
  local keyArg = 'pc' .. tostring(num) .. 'n'
  local valArg = 'pc' .. tostring(num) .. 'v'
  local key = args[keyArg]
  key = tonumber(key) or key
  local value = args[valArg]
  constantArgs[key] = value
 end
 return constantArgs
end

function p.getVariableVals(args)
 local variableVals = {}
 if args.start or args.stop or args.by then
  if args[2] then
   error("Both start/stop/by and numbered parameters specified")
  end
  local start = tonumber(args.start or 1)
  local stop = tonumber(args.stop or 1)
  local by = tonumber(args.by or 1)
  for i = start, stop, by do
   variableVals [#variableVals + 1] = i
  end
 else
  for i, v in ipairs(args) do
   if i ~= 1 then
    variableVals[i - 1] = v
   end
  end
 end
 return variableVals
end

function p.getArgNums(args, prefix, suffix)
 -- Returns a table containing the numbers of the arguments that exist
 -- for the specified prefix and suffix.
 local nums = {}
 local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'
 for k, _ in pairs(args) do
  local num = tostring(k):match(pattern)
  if num then
   nums[#nums + 1] = tonumber(num)
  end
 end
 table.sort(nums)
 return nums
end

function p.callTemplate(template, targs)
 return mw.getCurrentFrame():expandTemplate{title = template, args = targs}
end

return p

Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:For_loop&oldid=958167566"

Categories: 
Modules for general use
Modules subject to page protection
Hidden category: 
Wikipedia template-protected modules
 



This page was last edited on 22 May 2020, at 07:38 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki