 |
|
Command : Foreach
|
|
|
Syntax
FOREACH ( < variable > , < string-list > ) < command >
Description
This is an iteration command useful for scripting.
Many functions will return you comma separated lists of items like nick-lists or channel-lists.
Foreach is used to iterate a command between all the list elements.
The < variable > is created when this command is called , and is set internally
at each iteration to the next element in the list.
The loop ends when all list elements have been parsed.
If you need to break the loop use the HALT command , adjust the list
before executing the loop or use a conditional expression in the command.
WARNING : The < variable > is UNIQUE to all kvirc commands/scripts/events.
That means that if you call in the < command > an alias that uses (or unsets) the
< variable > , the remaining part of the < command > will 'see' the variable
changed!
This may also be dangerous when using recursive calls...
Be careful with that :) , better to have a differen variable for every FOREACH call in
your script.
Unsetting the variable in the loop may also be dangerous.
In the Event_Handlers or Aliases you can use also the $0,$1,$2... variables as 'item-containers'.
This will speed up a bit the execution of the command , so I reccomend to do it when possible.
Example
/FOREACH ($tempvar,1,2,3,4,5,6,7,8,9) echo $_window Counting $tempvar
/FOREACH ($tmp,$%chn_allonchan[$_chan])
say $tmp Hello...I'm back.
/FOREACH ($0,$%chn_oponchan[$_chan]) <
     echo $_window Slapping $0;
     say $_chan $_mynick
slaps $0 with a 50 lb UNIX manual;
> ;
See also
IF WHILE
Index  Command list  Commandline syntax