Jump to content
Wikipedia The Free Encyclopedia

Module:For loop

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
Warning This Lua module is used on approximately 1,120,000 pages, or roughly 2% 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.

The above documentation is transcluded from Module:For loop/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

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

 localgetArgs=require('Module:Arguments').getArgs
 localyesno=require('Module:Yesno')
 localp={}

 functionp.templatemain(frame)
 returnp.main(frame:newChild{title="Template:For_loop"})
 end

 functionp.main(frame)
 localargs=getArgs(frame,{
 trim=false,
 removeBlanks=false
 })
 returnp._main(args)
 end

 functionp._main(args)
 localtemplate=args['call']or'void'
 localcalltemplates=yesno(args.substallor"",true)ornotmw.isSubsting()
 localvariableParam=args.pv
 variableParam=tonumber(variableParam)orvariableParamor1-- fix for positional parameters
 localvariableValPrefix=args.prefixor''
 localvariableValPostfix=args.postfixor''
 localsep=args[1]or''
 localconstantArgs=p.getConstants(args)
 localvariableVals=p.getVariableVals(args)

 localresult=''
 localaddSeparator=false;
 for_,vinipairs(variableVals)do
 v=mw.text.trim(v)
 if#v>0ornotyesno(args.skipBlanks)then
 ifaddSeparatorthen
 result=result..sep
 end
 addSeparator=true;
 localtargs=constantArgs
 targs[variableParam]=variableValPrefix..v..variableValPostfix
 ifcalltemplatesthen
 localoutput=p.callTemplate(template,targs)
 if#mw.text.trim(output)==0then
 addSeparator=false
 end
 result=result..output
 else
 localmakeTemplate=require('Module:Template invocation').invocation
 result=result..makeTemplate(template,targs)
 end
 end
 end
 returnresult
 end

 functionp.getConstants(args)
 localconstantArgNums=p.getArgNums(args,'pc','n')
 localconstantArgs={}
 for_,numinipairs(constantArgNums)do
 localkeyArg='pc'..tostring(num)..'n'
 localvalArg='pc'..tostring(num)..'v'
 localkey=args[keyArg]
 key=tonumber(key)orkey
 localvalue=args[valArg]
 constantArgs[key]=value
 end
 returnconstantArgs
 end

 functionp.getVariableVals(args)
 localvariableVals={}
 ifargs.startorargs.stoporargs.bythen
 ifargs[2]then
 error("Both start/stop/by and numbered parameters specified")
 end
 localstart=tonumber(args.startor1)
 localstop=tonumber(args.stopor1)
 localby=tonumber(args.byor1)
 fori=start,stop,bydo
 variableVals[#variableVals+1]=i
 end
 else
 fori,vinipairs(args)do
 ifi~=1then
 variableVals[i-1]=v
 end
 end
 end
 returnvariableVals
 end

 functionp.getArgNums(args,prefix,suffix)
 -- Returns a table containing the numbers of the arguments that exist
 -- for the specified prefix and suffix.
 localnums={}
 localpattern='^'..prefix..'([1-9]%d*)'..suffix..'$'
 fork,_inpairs(args)do
 localnum=tostring(k):match(pattern)
 ifnumthen
 nums[#nums+1]=tonumber(num)
 end
 end
 table.sort(nums)
 returnnums
 end

 functionp.callTemplate(template,targs)
 returnmw.getCurrentFrame():expandTemplate{title=template,args=targs}
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /