This plugin provides basic FOR and FOREACH loop constructions.

Syntax Rules

Tag Description
%FOREACH{"_var_" in="_list_"}% body %NEXT{"_var_"}% Loop over _body_ setting control variable _var_ to each successive element of _list_
%FOR{"_var_" start="_int_" stop="_int_" step="_int_"}% body %NEXT{"_var_"}% Loop over _body_ setting control variable _var_ to each integer in the range start to stop in steps of step

Parameters Description
"..." Loop control variable. Within the body of the text this may be referred to as $var. %FOREACH{"var" ...}% must have a matching %NEXT{"var"}%
in="..." Comma separated list. May contain TWiki Variables
"..." Loop control variable. Within the body of the text this may be referred to as $var. %FOR{"var" ...}% must have a matching %NEXT{"var"}%
"start"="..." Starting integer
"stop"="..." Stop integer
"step"="..." Increment/decrement var in steps of step. THIS IS REQUIRED

  • All parameters are required, and must appear in the specified order.

FOREACH Examples

Basic Usage

%FOREACH{"web" in="Main, Sandbox, TWiki"}%
   * [[$web.WebHome]]

... gives ...

... if installed.

Nested loops are possible:

%FOREACH{"i" in="1, 2, 3"}% %FOREACH{"j" in="a, b, c"}% $i$j %NEXT{"j"}% %NEXT{"i"}%
1a 1b 1c 2a 2b 2c 3a 3b 3c

Lists may contain TWiki Variables or other plugins.

  • The body of the loop may need to delay expansion: use $percnt to replace % if necessary.
%FOREACH{"web" in="%WEBLIST{"$name" separator=", "}%"}%
| $web | $percntFORMFIELD{"STATUS" topic="$web.%HOMETOPIC%" default="Formfield STATUS not defined in $web" alttext="Formfield STATUS not found in $web"}$percnt |
Aster Formfield STATUS not found in Aster
CfdTm Formfield STATUS not found in CfdTm
Main Formfield STATUS not found in Main
Saturne Formfield STATUS not found in Saturne
TWiki Formfield STATUS not found in TWiki

FOR Examples

Count up and down in steps

  • %FOR{"counta" start="1" stop="10" step="2"}% $counta %NEXT{"counta"}%
  • 1 3 5 7 9
  • %FOR{"countb" start="1" stop="10" step="1"}% $countb %NEXT{"countb"}%
  • 1 2 3 4 5 6 7 8 9 10
  • %FOR{"countc" start="10" stop="1" step="-1"}% $countc %NEXT{"countc"}%
  • 10 9 8 7 6 5 4 3 2 1

Nested loops are possible:

%FOR{"outer" start="1" stop="3" step="1"}%
%FOR{"inner" start="3" stop="1" step="-1"}%
   * $outer - $inner 
  • 1 - 103
  • 1 - 102
  • 1 - 101
  • 1 - 100
  • 2 - 103
  • 2 - 102
  • 2 - 101
  • 2 - 100
  • 3 - 103
  • 3 - 102
  • 3 - 101
  • 3 - 100

Error trapping

  • %FOR{"countd" start="10" stop="1" step="1"}% $countd %NEXT{"countd"}% (invalid step)
  • FOR countd : Bad step
  • %FOR{"counte" start="a" stop="b" step="c"}% $countd %NEXT{"counte"}% (non-integers)
  • FOR{"counte" start="a" stop="b" step="c"} : Not a number

Plugin Settings

Plugin settings are stored as preferences variables. To reference a plugin setting write %<plugin>_<setting>%, i.e. %INTERWIKIPLUGIN_SHORTDESCRIPTION%

  • One line description, is shown in the TextFormattingRules topic:
    • Set SHORTDESCRIPTION = Loop over successive elements of a list, or a range of numbers.

  • Debug plugin: (See output in data/debug.txt)
    • Set DEBUG = 0

Plugin Installation Instructions

Note: You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the server where TWiki is running.

  • Download the ZIP file from the Plugin web (see below)
  • Unzip in your twiki installation directory. Content:
    File: Description:
    data/TWiki/ForEachPlugin.txt Plugin topic
    data/TWiki/ForEachPlugin.txt,v Plugin topic repository
    lib/TWiki/Plugins/ Plugin Perl module
  • Test if the installation was successful:
    • enter samples here

Plugin Info

Plugin Author: TWiki:Main.MagnusLewisSmith
Plugin Version: 28 Sep 2005 (V1.000)
Change History:  
28 Sep 2005: Initial version
TWiki Dependency: $TWiki::Plugins::VERSION 1.024
CPAN Dependencies: none
Other Dependencies: none
Perl Version: 5.005
License: GPL (GNU General Public License)
TWiki:Plugins/Benchmark: GoodStyle nn%, FormattedSearch nn%, ForEachPlugin nn%
Plugin Home:

Related Topics: TWikiPreferences, TWikiPlugins

-- TWiki:Main.MagnusLewisSmith - 28 Sep 2005

Current Tags:
create new tag
, view all tags
Topic revision: r7 - 2005-09-29 - 02:16:27 - MagnusLewisSmith

Computational Fluid Dynamics and Turbulence Mechanics
@ the University of Manchester
This site is powered by the TWiki collaboration platformCopyright & by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Note: Please contribute updates to this topic on at TWiki:TWiki.ForEachPlugin