Error processing request

Parameters

CONTENT_LENGTH0
REQUEST_METHODGET
REQUEST_URI/revision/foreach?V=47
QUERY_STRINGV=47
CONTENT_TYPE
DOCUMENT_URI/revision/foreach
DOCUMENT_ROOT/var/www/nikit/nikit/nginx/../docroot
SCGI1
SERVER_PROTOCOLHTTP/1.1
HTTPSon
REMOTE_ADDR172.70.100.238
REMOTE_PORT41542
SERVER_PORT4443
SERVER_NAMEwiki.tcl-lang.org
HTTP_HOSTwiki.tcl-lang.org
HTTP_CONNECTIONKeep-Alive
HTTP_ACCEPT_ENCODINGgzip, br
HTTP_X_FORWARDED_FOR18.117.81.240
HTTP_CF_RAY88042ec34bc3e24f-ORD
HTTP_X_FORWARDED_PROTOhttps
HTTP_CF_VISITOR{"scheme":"https"}
HTTP_ACCEPT*/*
HTTP_USER_AGENTMozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; [email protected])
HTTP_CF_CONNECTING_IP18.117.81.240
HTTP_CDN_LOOPcloudflare
HTTP_CF_IPCOUNTRYUS

Body


Error

Unknow state transition: LINE -> END

-code

1

-level

0

-errorstack

INNER {returnImm {Unknow state transition: LINE -> END} {}} CALL {my render_wikit foreach **\ Summary\ **\n\nforeach\ -\ Iterate\ over\ all\ elements\ in\ one\ or\ more\ lists\n\n**\ Synopsis\ **\n\n\ \ \ \ :\ \ \ '''foreach'''\ ''varlist\ list\ body''\n\n\ \ \ \ :\ \ \ '''foreach'''\ ''varlist1\ list1''\ ?''varlist2\ list2\ ...\ varlistn\ listn''?\ ''body''\n\n\n\n\n\ \ \ \[http://www.tcl.tk/man/tcl/TclCmd/foreach.htm%|%official\ reference\ documentation\]:\ \ \ \n\n\n\n\n\ \ \ \[for\]:\ \ \ \n\n\ \ \ \[while\]:\ \ \ \n\n\ \ \ \[foreach\ little\ friends\]:\ \ \ \n\n\ \ \ \[http://paste.tclers.tk/3012%|%foreach\ with\ coroutines\],\ by\ \[KBK\]:\ \ \ \n\ \ \ \[for\ in%|%for\ ...\ in\]:\ \ \ \n\n\n\n`\[foreach\]`\ implements\ a\ loop\ where\ the\ loop\ variable(s)\ take\ on\ values\ from\n\[\[`\[foreach\]`\]\ implements\ a\ loop\ where\ the\ loop\ variable(s)\ take\ on\ values\ from\n''varname'',\ and\ one\ list,\ ''listn'',\ and\ with\ each\ iteration,\ the\ next\ item\ in\n''list''\ is\ assigned\ to\ ''varname''.\ \ ''body''\ is\ a\ Tcl\ script\ that\ is\nevaluated\ once\ for\ each\ iteration\ of\ the\ loop.\ \n\nAlthough\ any\ of\ the\ lists\ passed\ as\ arguments\ can\ be\ modified\ by\ ''body'',\ the\nchanges\ will\ not\ be\ seen\ by\ `\[foreach\]`\ as\ the\ loop\ iterations\ continue.\nchanges\ will\ not\ be\ seen\ by\ \[\[`\[foreach\]`\]\ as\ the\ loop\ iterations\ continue.\npreserve\ this\ behaviour.\ \ If\ a\ list\ is\ large,\ modifying\ it\ within\ ''body''\ can\ntherefore\ have\ an\ adverse\ impact\ on\ memory.\n\nTo\ take\ multiple\ items\ from\ the\ list\ at\ each\ iteration,\ supply\ multiple\ variable\ names\ in\ ''varlist'':\n\n======\nforeach\ \{name\ gender\}\ \[list\ Bob\ male\ Joe\ male\ Sarah\ female\ Kim\ unknown\]\ \{\nforeach\ \{name\ gender\}\ \[list\ Bob\ male\ Joe\ male\ Sarah\ female\ Kim\ hermaphrodite\]\ \{\n\}\n======\n\n\nMultiple\ lists\ can\ be\ traversed\ in\ simultaneously\ by\ adding\ them\ as\ additional\narguments\ to\ the\ command.\ During\ each\ iteration\ of\ the\ loop,\ subsequent\ items\nfrom\ each\ list\ are\ assigned\ to\ the\ corresponding\ variables.\ Items\ in\ each\n''list''\ are\ used\ in\ order\ from\ first\ to\ last,\ and\ each\ item\ is\ used\ exactly\nonce.\n\n======\n\$\ tclsh\n%\ foreach\ a\ \[list\ 1\ 2\ 3\ 4\]\ b\ \[list\ 5\ 6\ 7\ 8\]\ c\ \[list\ a\ b\ c\ d\]\ d\ \[list\ w\ x\ y\ z\]\ \{\n\ \ \ \ \ \ puts\ \"\$a\ \$b\ \$c\ \$d\"\n\ \ \}\n1\ 5\ a\ w\n2\ 6\ b\ x\n3\ 7\ c\ y\n4\ 8\ d\ z\n%\n======\n\nCombining\ multiple\ variable\ names\ and\ multiple\ lists:\n\n======\nforeach\ \{fname\ lname\}\ \[join\ \$first_and_last_names\]\ phone\ \$phone_numbers\ \{\nforeach\ \{\ fname\ lname\ \}\ \[join\ \$first_and_last_names\]\ phone\ \$phone_numbers\ \{\n\}\n======\n\n\nThe\ total\ number\ of\ loop\ iterations\ is\ large\ enough\ to\ use\ up\ all\ the\ values\nfrom\ all\ the\ lists.\ \ If\ a\ list\ does\ not\ contain\ enough\ items\ for\ each\ of\ its\nloop\ variables\ in\ each\ iteration,\ empty\ values\ are\ used\ for\ the\ missing\nelements:\n\n======\n%\ unset\ -nocomplain\ a\ b\ c\n%\ foreach\ \{a\ b\ c\}\ \{1\ 2\}\ \{break\}\n%\ info\ exists\ c\n1\n======\n\n\n`\[break\]`\ and\ `\[continue\]`\ may\ be\ invoked\ inside\ ''body'',\ and\ have\ the\n\[\[`\[break\]`\]\]\ and\ \[\[`\[continue\]`\]\ may\ be\ invoked\ inside\ ''body'',\ and\ have\ the\nsame\ effect\ as\ in\ \[\[`\[for\]`\].\ \[\[`foreach`\]\ returns\ an\ empty\ string.\ \ (From:\n\nIf\ all\ the\ ''list''\ arguments\ are\ empty\ lists,\ no\ assignment\ to\ the\ncorresponding\ variables\ occurs,\ and\ ''body''\ is\ never\ evaluated:\n\n======\n%\ unset\ -nocomplain\ a\ b\ c\n%\ foreach\ \{a\ b\ c\}\ \{\}\ \{break\;\}\n%\ info\ exists\ a\n0\n======\n\nThe\ documentation\ could\ be\ interpreted\ as\ implying\ that\ the\ corresponding\nvariables\ would\ receive\ empty\ values:\n\n\ \ \ \ :\ \ \ If\ a\ value\ list\ does\ not\ contain\ enough\ elements\ for\ each\ of\ its\ loop\ variables\ in\ each\ iteration,\ empty\ values\ are\ used\ for\ the\ missing\ elements.''\n\nbut\ then\ it\ goes\ on\ to\ dispel\ that\ interpretation,\ since\ zero\ iterations\ is\nlarge\ enough\ to\ iterate\ over\ zero\ items:\n\n\ \ \ \ :\ \ \ The\ total\ number\ of\ loop\ iterations\ is\ large\ enough\ to\ use\ up\ all\ the\ values\ from\ all\ the\ value\ lists.\ If\ a\ value\ list\ ...\n\n\n\n**\ Examples\ **\n\n======\nset\ l\ \[list\ a\ bc\ def\ 1\ 23\ 456\]\nset\ m\ 0\n\nforeach\ i\ \$l\ \{\n\ \ \ \ incr\ m\n\ \ \ \ puts\ \"member\ \$m\ is\ \$i\"\n\}\n======\n\noutput:\n\n======\nmember\ 1\ is\ a\nmember\ 2\ is\ bc\nmember\ 3\ is\ def\nmember\ 4\ is\ 1\nmember\ 5\ is\ 23\nmember\ 6\ is\ 456\n======\n\n----\nTo\ stride\ through\ a\ list,\ picking\ up\ every\ n'th\ item,\ a\ dummy\ variable\ like\ `\$-`\n**\ \[\[`foreach`\]\]\ as\ an\ enhanced\ \[\[`\[set\]`\]\ **\n\nOne\ popular\ \[idiom\]\ is\ to\ use\ the\ varlist\ assignments\ to\ split\ up\ a\ list.\ The\nbody\ in\ such\ cases\ would\ just\ be\ \[\[`\[break\]`\]\ to\ exit\ \[\[`foreach`\]\ on\ the\ first\niteration:\n\n======\nforeach\ \{first\ second\ third\}\ \$list\ break\ \;#\ RS\n======\n\nA\ similar\ pattern\ can\ be\ used\ to\ swap\ variable\ values:\n\n======\nforeach\ \{a\ b\}\ \[list\ \$b\ \$a\]\ break\n======\n\nIt\ might\ be\ tempting\ to\ abbreviate\ the\ above\ to\ `foreach\ a\ \$b\ b\ \$a\ break`,\ but\n\[don't\ do\ that\]\;\ it\ really\ sets\ `b`\ to\ the\ result\ of\ `\[\[\[lindex\]\ \$a\ 0\]\]`\ and\nvice\ versa,\ which\ in\ general\ is\ something\ quite\ different,\ and\ even\ when\ it\nisn't\ it\ will\ result\ in\ \[shimmering\].\n\n\[AMG\]:\ \[\[`\[lassign\]`\]\ can\ do\ both\ these\ tasks.\n\n======\nlassign\ \$list\ first\ second\ third\nlassign\ \[list\ \$b\ \$a\]\ a\ b\n======\n\nUnlike\ \[\[`foreach`\],\ it\ returns\ any\ unused\ elements\ from\ its\ first\ argument.\nThis\ makes\ it\ useful\ for\ detecting\ extra\ arguments\ or\ popping\ elements\ off\ a\nstack.\ \ Also\ unlike\ \[\[foreach\]\],\ it\ won't\ create\ variables\ having\ an\ empty\nstring\ value\ when\ its\ first\ argument\ doesn't\ have\ enough\ elements.\ \ This\ makes\nit\ useful\ for\ detecting\ insufficient\ arguments.\n\n\n**\ Examples\ **\n\nTo\ stride\ through\ a\ list,\ picking\ up\ every\ n'th\ item,\ a\ dummy\ variable\ like\ \"-\"\n\n======\nforeach\ \{-\ i\}\ \$list\ \{\nforeach\ \{-\ i\}\ \$list\ \{...\}\n\nTo\ save\ an\ array\ to\ a\ file:\n\n======\nforeach\ \{key\ value\}\ \[array\ get\ myArray\]\ \{\n\ \ \ \ puts\ \$myFile\ \[list\ set\ myArray(\$key)\ \$value\]\n\}\n======\n\nOf\ course,\ you\ could\ have\ written:\n\n======\nputs\ \$myFile\ \[list\ array\ set\ myArray\ \[array\ get\ myArray\]\]\n======\n\nbut\ when\ editing\ such\ a\ file,\ the\ lack\ of\ line\ endings\ might\ give\ your\ text\neditor\ indigestion.\n\nTo\ save\ an\ array\ to\ a\ file,\ ordering\ it\ alphabetically\ by\ key:\n\n======\nforeach\ \{key\}\ \[lsort\ -dictionary\ \[array\ names\ myArray\]\]\ \{\n\ \ \ \ puts\ \$myFile\ \[list\ set\ myArray(\$key)\ \$myArray(\$key)\]\n\}\n======\n----\n\nIn\ a\ canvas,\ applying\ \[\[`\[foreach\]`\]\ to\ the\ result\ of\ the\ \[\[`\[bbox\]`\]\ is\ often\nthe\ idea.\n\n======\ngrid\ \[canvas\ .c\ -width\ 400\ -height\ 300\ -bg\ gray85\]\nset\ i\ \[.c\ create\ text\ 200\ 50\ -text\ \"One\"\ -anchor\ n\]\nforeach\ item\ \{\ Two\ Three\ Four\ \}\ \{\n\ \ \ \ foreach\ \{\ -\ -\ -\ y\ \}\ \[.c\ bbox\ \$i\]\ break\n\ \ \ \ set\ nexty\ \[expr\ \{\ \$y\ +\ 50\ \}\]\n\ \ \ \ .c\ create\ line\ 200\ \$y\ 200\ \$nexty\ -arrow\ last\n\ \ \ \ set\ i\ \[.c\ create\ text\ 200\ \$nexty\ -text\ \$item\ -anchor\ n\]\n\}\n======\n\[foreachOnbbox\]\n\[KPV\]:\ \ A\ similar\ example\ illustrates\ finding\ the\ dimensions\ of\ a\ canvas\ to\nshrink\ the\ size\ to\ provide\ a\ margin\ around\ the\ layout:\n\n======\n#\ Get\ canvas\ dimensions\ shrunk\ by\ some\nforeach\ who\ \{x0\ y0\ x1\ y1\}\ val\ \[.c\ cget\ -scrollregion\]\ d\ \{30\ 30\ -30\ -30\}\ \{\n\ \ \ \ set\ \$who\ \[expr\ \{\$val\ +\ \$d\}\]\n\}\n======\n\n\[http://img684.imageshack.us/img684/1269/image71.gif\]\n\n\n\n\n`\[foreach\]`\ can\ be\ used\ as\ a\ code\ block\ that\ runs\ exactly\ once,\ but\n\[\[`foreach`\]\ can\ be\ used\ as\ a\ code\ block\ that\ runs\ that\ runs\ exactly\ once,\ but\ncan\ be\ exited\ from\ with\ \[\[`\[break\]`\]\ or\ \[\[`\[continue\]`\]\ (seen\ in\ a\ c.l.t\ post\n\n======\nforeach\ _\ _\ \{\n\ \ \ \ #\ some\ processing\n\ \ \ \ if\ \{\$somecondition\}\ break\n\ \ \ \ #\ some\ other\ processing\n\ \ \ \ if\ \{\$someothercondition\}\ break\n\ \ \ \ #\ still\ more\ processing\n\}\n======\n\nThis\ is\ an\ alternative\ to\ nested\ `\[if\]`\ structures.\nThis\ is\ an\ alternative\ to\ nested\ \[\[`\[if\]`\]\ structures.\n\[RS\]\ is\ not\ sure\ whether\ to\ recommend\ this\ style..\ but\ proof\ again\ that\ you\ can\ndo\ more\ with\ Tcl\ than\ you'd\ imagine...\;-)\ Well\ hell,\ then\ add\ some\ sugar:\n\n======\ninterp\ alias\ \{\}\ breakable\ \{\}\ foreach\ _\ _\nbreakable\ \{\ \;#\ or\ 'fragile'?\n\ \ \ \ #\ some\ processing\n\ \ \ \ if\ \{\$somecondition\}\ break\n\ \ \ \ #\ some\ other\ processing\n\ \ \ \ if\ \{\$someothercondition\}\ break\n\ \ \ \ #\ still\ more\ processing\n\}\n======\n\nOr,\ to\ make\ your\ intention\ clearer:\n\n======\ninterp\ alias\ \{\}\ make\ \{\}\ foreach\nmake\ or\ break\ \{\n\ \ \ \ ...\n\}\n======\n\n\[AMG\]:\ This\ is\ like\ the\ '''`do\ \{...\}\ while\ (0)\;`'''\ idiom\ in\ \[C\],\ which\ is\nuseful\ not\ only\ in\ macros\ but\ also\ for\ creating\ a\ \"structured\ \[goto\]\"\ using\nbreak.\ \ Perhaps\ unfortunately,\ continue\ can't\ be\ used\ to\ go\ back\ to\ the\ top,\nsince\ it\ first\ checks\ the\ loop\ condition\;\ otherwise\ this\ idiom\ would\neffectively\ create\ two\ line\ labels\ at\ the\ same\ time.\ \;^)\ \ Very\ occasionally\ I\nwish\ for\ a\ \"redo\"\ statement\ that\ restarts\ the\ current\ iteration\ of\ the\ loop,\nbypassing\ both\ the\ loop\ condition\ and\ for's\ increment\ clause.\n\n\n\n**\ Development:\ What\ Could\ \[\[`foreach`\]\ Return?\ **\n\[SS\]\ wonders\ why\ foreach\ doesn't\ return\ the\ result\ of\ the\ last\ executed\ncommand.\ It\ can\ be\ useful\ for\ programmers\ using\ Tcl\ in\ a\ functional\ style.\ The\nsame\ also\ apply\ to\ `\[for\]`.\ \ \[CL\]\ likes\ the\ idea.\nsame\ also\ apply\ to\ \[\[`\[for\]`\].\ \ \[CL\]\ likes\ the\ idea.\n----\n\[rmax\]:\ Another\ useful\ return\ value\ (maybe\ even\ closer\ to\ functional\n\nprogramming)\ could\ be\ the\ unprocessed\ part\ of\ the\ list\ if\ a\ break\ occured\ in\nthe\ loop\ body.\n\nI\ sometimes\ find\ myself\ doing\ stuff\ like\ this:\n\n======\nforeach\ \{a\ b\ c\}\ \$args\ break\nset\ args\ \[lrange\ \$args\ 3\ end\]\n======\n\nThis\ could\ be\ done\ easier\ and\ would\ be\ less\ error\ prone\ if\ foreach\ returned\ the\ntail\ of\ the\ list:\n\n======\nset\ args\ \[foreach\ \{a\ b\ c\}\ \$args\ break\]\n======\n\nOf\ course,\ this\ feature\ can\ only\ work\ in\ a\ useful\ way\ if\ foreach\ was\ called\nwith\ just\ one\ list.\n\n\[RS\]:\ This\ is\ what\ `\[lassign\]`\ (from\ 8.5,\ or\ \[TclX\])\ does:\n\[RS\]:\ This\ is\ what\ \[\[`\[lassign\]`\]\ (from\ 8.5,\ or\ \[TclX\])\ does:\n======\nset\ args\ \[lassign\ \$args\ a\ b\ c\]\n======\n\n----\n\[SS\]:\ \ Yep\ \[rmax\],\ my\ past\ idea\ evolved\ almost\ exactly\ into\ a\ new\ idea\ similar\n\nto\ your\ suggestion.\ \ Actually\ I'm\ not\ sure\ about\ many\ different\ options:\n\n\ \ \ OPTION\ 1:\ \ \ `\[foreach\]`\ always\ returns\ the\ return\ value\ of\ the\ last\ command\ executed,\ but\ can\ be\ called\ without\ the\ 'script'\ argument,\ and\ in\ such\ a\ case\ the\ variables\ are\ assigned,\ and\ foreach\ returns\ the\ rest\ of\ the\ list,\ so:\n\ \ \ OPTION\ 1:\ \ \ \[\[`foreach`\]\ always\ returns\ the\ return\ value\ of\ the\ last\ command\ executed,\nbut\ can\ be\ called\ without\ the\ 'script'\ argument,\ and\ in\ such\ a\ case\ the\nvariables\ are\ assigned,\ and\ foreach\ returns\ the\ rest\ of\ the\ list,\ so:\n======\nset\ args\ \[foreach\ \{a\ b\ c\}\ \$args\]\n======\n\ndoes\ what\ your\ example\ do.\n\n\ \ \ OPTION\ 2:\ \ \ `\[foreach\]`\ always\ returns\ the\ rest\ of\ the\ list\ (with\ rest\ I\ mean\ the\ ''not\ processed\ part'').\ \ The\ last\ argument\ can\ be\ omitted\ and\ defaults\ to\ `\[break\]`.\n\ \ \ OPTION\ 2:\ \ \ \[\[`foreach`\]\ always\ returns\ the\ rest\ of\ the\ list\ (with\ rest\ I\ mean\ the\ ''not\ processed\ part'').\ \ The\ last\ argument\ can\ be\ omitted\ and\ defaults\ to\ \[\[`\[break\]\].\n======\nset\ args\ \[foreach\ \{a\ b\ c\}\ \$args\]\n======\n\nis\ still\ valid.\n\n\ \ \ OPTION\ 3:\ \ \ `\[foreach\]`\ works\ exactly\ like\ today,\ but\ works\ as\ OPTION\ 1\ when\ called\ without\ the\ script\ argument.\ That's\ fully\ backward\ compatible,\ but\ less\ orthogonal\ and\ less\ powerful.\n\ \ \ OPTION\ 3:\ \ \ \[\[`foreach`\]\ works\ exactly\ like\ today,\ but\ works\ as\ OPTION\ 1\ when\ called\ without\ the\ script\ argument.\ That's\ fully\ backward\ compatible,\ but\ less\ orthogonal\ and\ less\ powerful.\n----\n<<categories>>\ Syntax\ |\ Arts\ and\ Crafts\ of\ Tcl-Tk\ Programming\ |\ Command\ |\ Control\ Structure\n\n\n<<categories>>\ Tcl\ syntax\ help\ |\ Arts\ and\ Crafts\ of\ Tcl-Tk\ Programming\ |\ Command\ |\ Control\ Structure regexp2} CALL {my render foreach **\ Summary\ **\n\nforeach\ -\ Iterate\ over\ all\ elements\ in\ one\ or\ more\ lists\n\n**\ Synopsis\ **\n\n\ \ \ \ :\ \ \ '''foreach'''\ ''varlist\ list\ body''\n\n\ \ \ \ :\ \ \ '''foreach'''\ ''varlist1\ list1''\ ?''varlist2\ list2\ ...\ varlistn\ listn''?\ ''body''\n\n\n\n\n\ \ \ \[http://www.tcl.tk/man/tcl/TclCmd/foreach.htm%|%official\ reference\ documentation\]:\ \ \ \n\n\n\n\n\ \ \ \[for\]:\ \ \ \n\n\ \ \ \[while\]:\ \ \ \n\n\ \ \ \[foreach\ little\ friends\]:\ \ \ \n\n\ \ \ \[http://paste.tclers.tk/3012%|%foreach\ with\ coroutines\],\ by\ \[KBK\]:\ \ \ \n\ \ \ \[for\ in%|%for\ ...\ in\]:\ \ \ \n\n\n\n`\[foreach\]`\ implements\ a\ loop\ where\ the\ loop\ variable(s)\ take\ on\ values\ from\n\[\[`\[foreach\]`\]\ implements\ a\ loop\ where\ the\ loop\ variable(s)\ take\ on\ values\ from\n''varname'',\ and\ one\ list,\ ''listn'',\ and\ with\ each\ iteration,\ the\ next\ item\ in\n''list''\ is\ assigned\ to\ ''varname''.\ \ ''body''\ is\ a\ Tcl\ script\ that\ is\nevaluated\ once\ for\ each\ iteration\ of\ the\ loop.\ \n\nAlthough\ any\ of\ the\ lists\ passed\ as\ arguments\ can\ be\ modified\ by\ ''body'',\ the\nchanges\ will\ not\ be\ seen\ by\ `\[foreach\]`\ as\ the\ loop\ iterations\ continue.\nchanges\ will\ not\ be\ seen\ by\ \[\[`\[foreach\]`\]\ as\ the\ loop\ iterations\ continue.\npreserve\ this\ behaviour.\ \ If\ a\ list\ is\ large,\ modifying\ it\ within\ ''body''\ can\ntherefore\ have\ an\ adverse\ impact\ on\ memory.\n\nTo\ take\ multiple\ items\ from\ the\ list\ at\ each\ iteration,\ supply\ multiple\ variable\ names\ in\ ''varlist'':\n\n======\nforeach\ \{name\ gender\}\ \[list\ Bob\ male\ Joe\ male\ Sarah\ female\ Kim\ unknown\]\ \{\nforeach\ \{name\ gender\}\ \[list\ Bob\ male\ Joe\ male\ Sarah\ female\ Kim\ hermaphrodite\]\ \{\n\}\n======\n\n\nMultiple\ lists\ can\ be\ traversed\ in\ simultaneously\ by\ adding\ them\ as\ additional\narguments\ to\ the\ command.\ During\ each\ iteration\ of\ the\ loop,\ subsequent\ items\nfrom\ each\ list\ are\ assigned\ to\ the\ corresponding\ variables.\ Items\ in\ each\n''list''\ are\ used\ in\ order\ from\ first\ to\ last,\ and\ each\ item\ is\ used\ exactly\nonce.\n\n======\n\$\ tclsh\n%\ foreach\ a\ \[list\ 1\ 2\ 3\ 4\]\ b\ \[list\ 5\ 6\ 7\ 8\]\ c\ \[list\ a\ b\ c\ d\]\ d\ \[list\ w\ x\ y\ z\]\ \{\n\ \ \ \ \ \ puts\ \"\$a\ \$b\ \$c\ \$d\"\n\ \ \}\n1\ 5\ a\ w\n2\ 6\ b\ x\n3\ 7\ c\ y\n4\ 8\ d\ z\n%\n======\n\nCombining\ multiple\ variable\ names\ and\ multiple\ lists:\n\n======\nforeach\ \{fname\ lname\}\ \[join\ \$first_and_last_names\]\ phone\ \$phone_numbers\ \{\nforeach\ \{\ fname\ lname\ \}\ \[join\ \$first_and_last_names\]\ phone\ \$phone_numbers\ \{\n\}\n======\n\n\nThe\ total\ number\ of\ loop\ iterations\ is\ large\ enough\ to\ use\ up\ all\ the\ values\nfrom\ all\ the\ lists.\ \ If\ a\ list\ does\ not\ contain\ enough\ items\ for\ each\ of\ its\nloop\ variables\ in\ each\ iteration,\ empty\ values\ are\ used\ for\ the\ missing\nelements:\n\n======\n%\ unset\ -nocomplain\ a\ b\ c\n%\ foreach\ \{a\ b\ c\}\ \{1\ 2\}\ \{break\}\n%\ info\ exists\ c\n1\n======\n\n\n`\[break\]`\ and\ `\[continue\]`\ may\ be\ invoked\ inside\ ''body'',\ and\ have\ the\n\[\[`\[break\]`\]\]\ and\ \[\[`\[continue\]`\]\ may\ be\ invoked\ inside\ ''body'',\ and\ have\ the\nsame\ effect\ as\ in\ \[\[`\[for\]`\].\ \[\[`foreach`\]\ returns\ an\ empty\ string.\ \ (From:\n\nIf\ all\ the\ ''list''\ arguments\ are\ empty\ lists,\ no\ assignment\ to\ the\ncorresponding\ variables\ occurs,\ and\ ''body''\ is\ never\ evaluated:\n\n======\n%\ unset\ -nocomplain\ a\ b\ c\n%\ foreach\ \{a\ b\ c\}\ \{\}\ \{break\;\}\n%\ info\ exists\ a\n0\n======\n\nThe\ documentation\ could\ be\ interpreted\ as\ implying\ that\ the\ corresponding\nvariables\ would\ receive\ empty\ values:\n\n\ \ \ \ :\ \ \ If\ a\ value\ list\ does\ not\ contain\ enough\ elements\ for\ each\ of\ its\ loop\ variables\ in\ each\ iteration,\ empty\ values\ are\ used\ for\ the\ missing\ elements.''\n\nbut\ then\ it\ goes\ on\ to\ dispel\ that\ interpretation,\ since\ zero\ iterations\ is\nlarge\ enough\ to\ iterate\ over\ zero\ items:\n\n\ \ \ \ :\ \ \ The\ total\ number\ of\ loop\ iterations\ is\ large\ enough\ to\ use\ up\ all\ the\ values\ from\ all\ the\ value\ lists.\ If\ a\ value\ list\ ...\n\n\n\n**\ Examples\ **\n\n======\nset\ l\ \[list\ a\ bc\ def\ 1\ 23\ 456\]\nset\ m\ 0\n\nforeach\ i\ \$l\ \{\n\ \ \ \ incr\ m\n\ \ \ \ puts\ \"member\ \$m\ is\ \$i\"\n\}\n======\n\noutput:\n\n======\nmember\ 1\ is\ a\nmember\ 2\ is\ bc\nmember\ 3\ is\ def\nmember\ 4\ is\ 1\nmember\ 5\ is\ 23\nmember\ 6\ is\ 456\n======\n\n----\nTo\ stride\ through\ a\ list,\ picking\ up\ every\ n'th\ item,\ a\ dummy\ variable\ like\ `\$-`\n**\ \[\[`foreach`\]\]\ as\ an\ enhanced\ \[\[`\[set\]`\]\ **\n\nOne\ popular\ \[idiom\]\ is\ to\ use\ the\ varlist\ assignments\ to\ split\ up\ a\ list.\ The\nbody\ in\ such\ cases\ would\ just\ be\ \[\[`\[break\]`\]\ to\ exit\ \[\[`foreach`\]\ on\ the\ first\niteration:\n\n======\nforeach\ \{first\ second\ third\}\ \$list\ break\ \;#\ RS\n======\n\nA\ similar\ pattern\ can\ be\ used\ to\ swap\ variable\ values:\n\n======\nforeach\ \{a\ b\}\ \[list\ \$b\ \$a\]\ break\n======\n\nIt\ might\ be\ tempting\ to\ abbreviate\ the\ above\ to\ `foreach\ a\ \$b\ b\ \$a\ break`,\ but\n\[don't\ do\ that\]\;\ it\ really\ sets\ `b`\ to\ the\ result\ of\ `\[\[\[lindex\]\ \$a\ 0\]\]`\ and\nvice\ versa,\ which\ in\ general\ is\ something\ quite\ different,\ and\ even\ when\ it\nisn't\ it\ will\ result\ in\ \[shimmering\].\n\n\[AMG\]:\ \[\[`\[lassign\]`\]\ can\ do\ both\ these\ tasks.\n\n======\nlassign\ \$list\ first\ second\ third\nlassign\ \[list\ \$b\ \$a\]\ a\ b\n======\n\nUnlike\ \[\[`foreach`\],\ it\ returns\ any\ unused\ elements\ from\ its\ first\ argument.\nThis\ makes\ it\ useful\ for\ detecting\ extra\ arguments\ or\ popping\ elements\ off\ a\nstack.\ \ Also\ unlike\ \[\[foreach\]\],\ it\ won't\ create\ variables\ having\ an\ empty\nstring\ value\ when\ its\ first\ argument\ doesn't\ have\ enough\ elements.\ \ This\ makes\nit\ useful\ for\ detecting\ insufficient\ arguments.\n\n\n**\ Examples\ **\n\nTo\ stride\ through\ a\ list,\ picking\ up\ every\ n'th\ item,\ a\ dummy\ variable\ like\ \"-\"\n\n======\nforeach\ \{-\ i\}\ \$list\ \{\nforeach\ \{-\ i\}\ \$list\ \{...\}\n\nTo\ save\ an\ array\ to\ a\ file:\n\n======\nforeach\ \{key\ value\}\ \[array\ get\ myArray\]\ \{\n\ \ \ \ puts\ \$myFile\ \[list\ set\ myArray(\$key)\ \$value\]\n\}\n======\n\nOf\ course,\ you\ could\ have\ written:\n\n======\nputs\ \$myFile\ \[list\ array\ set\ myArray\ \[array\ get\ myArray\]\]\n======\n\nbut\ when\ editing\ such\ a\ file,\ the\ lack\ of\ line\ endings\ might\ give\ your\ text\neditor\ indigestion.\n\nTo\ save\ an\ array\ to\ a\ file,\ ordering\ it\ alphabetically\ by\ key:\n\n======\nforeach\ \{key\}\ \[lsort\ -dictionary\ \[array\ names\ myArray\]\]\ \{\n\ \ \ \ puts\ \$myFile\ \[list\ set\ myArray(\$key)\ \$myArray(\$key)\]\n\}\n======\n----\n\nIn\ a\ canvas,\ applying\ \[\[`\[foreach\]`\]\ to\ the\ result\ of\ the\ \[\[`\[bbox\]`\]\ is\ often\nthe\ idea.\n\n======\ngrid\ \[canvas\ .c\ -width\ 400\ -height\ 300\ -bg\ gray85\]\nset\ i\ \[.c\ create\ text\ 200\ 50\ -text\ \"One\"\ -anchor\ n\]\nforeach\ item\ \{\ Two\ Three\ Four\ \}\ \{\n\ \ \ \ foreach\ \{\ -\ -\ -\ y\ \}\ \[.c\ bbox\ \$i\]\ break\n\ \ \ \ set\ nexty\ \[expr\ \{\ \$y\ +\ 50\ \}\]\n\ \ \ \ .c\ create\ line\ 200\ \$y\ 200\ \$nexty\ -arrow\ last\n\ \ \ \ set\ i\ \[.c\ create\ text\ 200\ \$nexty\ -text\ \$item\ -anchor\ n\]\n\}\n======\n\[foreachOnbbox\]\n\[KPV\]:\ \ A\ similar\ example\ illustrates\ finding\ the\ dimensions\ of\ a\ canvas\ to\nshrink\ the\ size\ to\ provide\ a\ margin\ around\ the\ layout:\n\n======\n#\ Get\ canvas\ dimensions\ shrunk\ by\ some\nforeach\ who\ \{x0\ y0\ x1\ y1\}\ val\ \[.c\ cget\ -scrollregion\]\ d\ \{30\ 30\ -30\ -30\}\ \{\n\ \ \ \ set\ \$who\ \[expr\ \{\$val\ +\ \$d\}\]\n\}\n======\n\n\[http://img684.imageshack.us/img684/1269/image71.gif\]\n\n\n\n\n`\[foreach\]`\ can\ be\ used\ as\ a\ code\ block\ that\ runs\ exactly\ once,\ but\n\[\[`foreach`\]\ can\ be\ used\ as\ a\ code\ block\ that\ runs\ that\ runs\ exactly\ once,\ but\ncan\ be\ exited\ from\ with\ \[\[`\[break\]`\]\ or\ \[\[`\[continue\]`\]\ (seen\ in\ a\ c.l.t\ post\n\n======\nforeach\ _\ _\ \{\n\ \ \ \ #\ some\ processing\n\ \ \ \ if\ \{\$somecondition\}\ break\n\ \ \ \ #\ some\ other\ processing\n\ \ \ \ if\ \{\$someothercondition\}\ break\n\ \ \ \ #\ still\ more\ processing\n\}\n======\n\nThis\ is\ an\ alternative\ to\ nested\ `\[if\]`\ structures.\nThis\ is\ an\ alternative\ to\ nested\ \[\[`\[if\]`\]\ structures.\n\[RS\]\ is\ not\ sure\ whether\ to\ recommend\ this\ style..\ but\ proof\ again\ that\ you\ can\ndo\ more\ with\ Tcl\ than\ you'd\ imagine...\;-)\ Well\ hell,\ then\ add\ some\ sugar:\n\n======\ninterp\ alias\ \{\}\ breakable\ \{\}\ foreach\ _\ _\nbreakable\ \{\ \;#\ or\ 'fragile'?\n\ \ \ \ #\ some\ processing\n\ \ \ \ if\ \{\$somecondition\}\ break\n\ \ \ \ #\ some\ other\ processing\n\ \ \ \ if\ \{\$someothercondition\}\ break\n\ \ \ \ #\ still\ more\ processing\n\}\n======\n\nOr,\ to\ make\ your\ intention\ clearer:\n\n======\ninterp\ alias\ \{\}\ make\ \{\}\ foreach\nmake\ or\ break\ \{\n\ \ \ \ ...\n\}\n======\n\n\[AMG\]:\ This\ is\ like\ the\ '''`do\ \{...\}\ while\ (0)\;`'''\ idiom\ in\ \[C\],\ which\ is\nuseful\ not\ only\ in\ macros\ but\ also\ for\ creating\ a\ \"structured\ \[goto\]\"\ using\nbreak.\ \ Perhaps\ unfortunately,\ continue\ can't\ be\ used\ to\ go\ back\ to\ the\ top,\nsince\ it\ first\ checks\ the\ loop\ condition\;\ otherwise\ this\ idiom\ would\neffectively\ create\ two\ line\ labels\ at\ the\ same\ time.\ \;^)\ \ Very\ occasionally\ I\nwish\ for\ a\ \"redo\"\ statement\ that\ restarts\ the\ current\ iteration\ of\ the\ loop,\nbypassing\ both\ the\ loop\ condition\ and\ for's\ increment\ clause.\n\n\n\n**\ Development:\ What\ Could\ \[\[`foreach`\]\ Return?\ **\n\[SS\]\ wonders\ why\ foreach\ doesn't\ return\ the\ result\ of\ the\ last\ executed\ncommand.\ It\ can\ be\ useful\ for\ programmers\ using\ Tcl\ in\ a\ functional\ style.\ The\nsame\ also\ apply\ to\ `\[for\]`.\ \ \[CL\]\ likes\ the\ idea.\nsame\ also\ apply\ to\ \[\[`\[for\]`\].\ \ \[CL\]\ likes\ the\ idea.\n----\n\[rmax\]:\ Another\ useful\ return\ value\ (maybe\ even\ closer\ to\ functional\n\nprogramming)\ could\ be\ the\ unprocessed\ part\ of\ the\ list\ if\ a\ break\ occured\ in\nthe\ loop\ body.\n\nI\ sometimes\ find\ myself\ doing\ stuff\ like\ this:\n\n======\nforeach\ \{a\ b\ c\}\ \$args\ break\nset\ args\ \[lrange\ \$args\ 3\ end\]\n======\n\nThis\ could\ be\ done\ easier\ and\ would\ be\ less\ error\ prone\ if\ foreach\ returned\ the\ntail\ of\ the\ list:\n\n======\nset\ args\ \[foreach\ \{a\ b\ c\}\ \$args\ break\]\n======\n\nOf\ course,\ this\ feature\ can\ only\ work\ in\ a\ useful\ way\ if\ foreach\ was\ called\nwith\ just\ one\ list.\n\n\[RS\]:\ This\ is\ what\ `\[lassign\]`\ (from\ 8.5,\ or\ \[TclX\])\ does:\n\[RS\]:\ This\ is\ what\ \[\[`\[lassign\]`\]\ (from\ 8.5,\ or\ \[TclX\])\ does:\n======\nset\ args\ \[lassign\ \$args\ a\ b\ c\]\n======\n\n----\n\[SS\]:\ \ Yep\ \[rmax\],\ my\ past\ idea\ evolved\ almost\ exactly\ into\ a\ new\ idea\ similar\n\nto\ your\ suggestion.\ \ Actually\ I'm\ not\ sure\ about\ many\ different\ options:\n\n\ \ \ OPTION\ 1:\ \ \ `\[foreach\]`\ always\ returns\ the\ return\ value\ of\ the\ last\ command\ executed,\ but\ can\ be\ called\ without\ the\ 'script'\ argument,\ and\ in\ such\ a\ case\ the\ variables\ are\ assigned,\ and\ foreach\ returns\ the\ rest\ of\ the\ list,\ so:\n\ \ \ OPTION\ 1:\ \ \ \[\[`foreach`\]\ always\ returns\ the\ return\ value\ of\ the\ last\ command\ executed,\nbut\ can\ be\ called\ without\ the\ 'script'\ argument,\ and\ in\ such\ a\ case\ the\nvariables\ are\ assigned,\ and\ foreach\ returns\ the\ rest\ of\ the\ list,\ so:\n======\nset\ args\ \[foreach\ \{a\ b\ c\}\ \$args\]\n======\n\ndoes\ what\ your\ example\ do.\n\n\ \ \ OPTION\ 2:\ \ \ `\[foreach\]`\ always\ returns\ the\ rest\ of\ the\ list\ (with\ rest\ I\ mean\ the\ ''not\ processed\ part'').\ \ The\ last\ argument\ can\ be\ omitted\ and\ defaults\ to\ `\[break\]`.\n\ \ \ OPTION\ 2:\ \ \ \[\[`foreach`\]\ always\ returns\ the\ rest\ of\ the\ list\ (with\ rest\ I\ mean\ the\ ''not\ processed\ part'').\ \ The\ last\ argument\ can\ be\ omitted\ and\ defaults\ to\ \[\[`\[break\]\].\n======\nset\ args\ \[foreach\ \{a\ b\ c\}\ \$args\]\n======\n\nis\ still\ valid.\n\n\ \ \ OPTION\ 3:\ \ \ `\[foreach\]`\ works\ exactly\ like\ today,\ but\ works\ as\ OPTION\ 1\ when\ called\ without\ the\ script\ argument.\ That's\ fully\ backward\ compatible,\ but\ less\ orthogonal\ and\ less\ powerful.\n\ \ \ OPTION\ 3:\ \ \ \[\[`foreach`\]\ works\ exactly\ like\ today,\ but\ works\ as\ OPTION\ 1\ when\ called\ without\ the\ script\ argument.\ That's\ fully\ backward\ compatible,\ but\ less\ orthogonal\ and\ less\ powerful.\n----\n<<categories>>\ Syntax\ |\ Arts\ and\ Crafts\ of\ Tcl-Tk\ Programming\ |\ Command\ |\ Control\ Structure\n\n\n<<categories>>\ Tcl\ syntax\ help\ |\ Arts\ and\ Crafts\ of\ Tcl-Tk\ Programming\ |\ Command\ |\ Control\ Structure} CALL {my revision foreach} CALL {::oo::Obj7581449 process revision/foreach} CALL {::oo::Obj7581447 process}

-errorcode

NONE

-errorinfo

Unknow state transition: LINE -> END
    while executing
"error $msg"
    (class "::Wiki" method "render_wikit" line 6)
    invoked from within
"my render_$default_markup $N $C $mkup_rendering_engine"
    (class "::Wiki" method "render" line 8)
    invoked from within
"my render $name $C"
    (class "::Wiki" method "revision" line 31)
    invoked from within
"my revision $page"
    (class "::Wiki" method "process" line 56)
    invoked from within
"$server process [string trim $uri /]"

-errorline

4