FOREACH

From HEWIKI
Jump to: navigation, search

FOREACH <item> IN <list>

Arguments

<item> 
A reference to the next item in the list. It is defined implicitly to be the same type of data as the list contains. Important: Attempting to pre-define <item> will generate a compile error.
<list> 
A list of some data type, that is being cycled through, item by item

Description

This is a HeroScript command that provides for/next loop functionality to scan through the items of a list or lookup list.

Care should be used when looping on a list, if the node that controls the list may possibly be modified within the loop structure (which might destroy the list, or add or remove items to the list). See below for an example.


Example

myList as list of string
foreach item in myList
  // reference item like a string variable
.

  x as list of class things_within_things
 
  foreach thingie in x                     // Check each node in X, setting ITEM in turn
    tgt as node
    tgt = thingie.target                  // Set TGT to a target node of the X association
    where tgt is exactly container         // Check its class
      if (thingie.target.name = "flask")
        flaskcount = (flaskcount + 1)
      .
    .
  .

        // it is acceptable to alter the list inside a foreach block
        // elements added before the current location will not be part of the foreach
foreach object in myList
  if (object = "something")
    add front "happy" to myList
  else if (myList[1] = "happy")
    remove myList at 1
  .
.
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox