foreach

foreach Iteration command
Usage
foreach [-a] (<variable>,[<item>[,<item>[,<item>[...]]]) <command>
Description

Executed <command> while assigning to <variable> each <item>.
<item> may be a constant, a variable, an array, a dictionary or a function returning either a constant string an array reference or a dictionary reference.
If <item> is an array, a dictionary or a function that returns a dictionary or array reference the iteration is done through all the dictionary/array items.
Please note that the iteration order of dictionary items is undefined.
You can always break from the loop by using the break command, or skip to the next item with continue.
foreach doesn't iterate over empty scalar variables (i.e. the ones set to $nothing) unless you use the -a switch. (Note that an array with *some* empty entries is not empty so the iteration is in fact done).

Switches

-a | --all
Include empty variables in the iteration loop.

Examples

foreach(%i,1,2,3,4,5,6,7,8,9) echo %i
foreach(%chan,$window.list(channel)) me -r=%chan This is a test!
# This will work too, and will do the same job
%windows[] = $window.list(channel)
foreach(%chan,%windows[]) me -r=%chan This is a test!
# And this too
%windows[] = $window.list(channel)
foreach(%key,$keys(%windows[])) me -r=%windows[%key] This is a test!
# Another interesting example
alias(test){ return $hash(1,a,2,b,3,c,4,d); };
foreach(%x,$keys($test)){ echo %x, $test{%x}; }

Index, Commands