repeat for each instruction

Description

The repeat for each instruction is used to iterate over items contained in an object of type iteratable (e.g. a list). The first pass in the for each loop gives access to the first item in the iteratable object, and each successive pass gives access to the next item. Hence, the number of passes is equal to the number of items in the iteratable object.

[Note]Note
The order in which the items are iterated depends on the iteratable object. The repeat for each instruction doesn't specify any order. For example, items in a map might not necessarily be iterated in the same order they have been added to the map. Please consult the iteratable's documentation to get information about the order items are iterated.

Syntax

Table 8.13. repeat for each syntax

ProductionSyntaxLinks
repeat_for_each_instruction

"repeat" "for" "each" ( "item" ? | "key" | "value" ) object_type_selector variable_id "in" expression repeat_tail ?
   script_instruction *
"end" "repeat" ?

remarks:

  • expression must be of type iteratable
  • object_type_selector must denote the type of the items in the iteratable object
  • "key" and "value" can be used to specify the iteration over keys or values in a map

the section called “repeat for each instruction”
repeat_tail( "counter" ":" variable_id ) ? ( "id" ":" identifier ) ?the section called “repeat tail”

Examples

Example 8.11. repeat for each example 1

// display numbers from 1 to 9

var string string_1_9 = "123456789"
repeat for each character c in string_1_9
   console.message ( c.to_string )
end

Example 8.12. repeat for each example 2

Suppose this type has been defined:

type product_4 default_factory:yes

   attribute identifier type:positive32 end
   attribute name type:string end
   attribute price type:zero_positive32 end
   
end

The following code will create a list of products and then iterate over them in order to find products with a price of 0:

service instruction_examples

   command repeat_for_each_example_2
      script
      
         // create list of products
         const !mutable_indexed_list<product_4> product_list = !mutable_indexed_list_factory<product_4>.co_create
         product_list.co_append ( fa_product_4.co_create ( &
            identifier = 100 &
            name = "banana" &
            price = 20 ) )
         product_list.co_append ( fa_product_4.co_create ( &
            identifier = 101 &
            name = "book" &
            price = 0 ) )
            
         // iterate over all products
         repeat for each product_4 item in product_list
            // check if price is 0
            if item.price =v 0 then
               se_console.co_message ( "Price of product " & item.a_identifier.co_to_string & " is 0." )
            end if
         end repeat

      end script
   end command
   
end service

The result on screen is:

Price of product 101 is 0.

Example 8.13. repeat for each example 3

The following code will create a string containing a TAB separated table of capitals, then store the string to a temporary file, restore that file into another string variable, and finally decompose the string into rows and columns:

service instruction_examples

   command repeat_for_each_example_3
      script
      
         // store TAB separated list of capitals into string
         const string tab = se_string.a_tab
         const string capitals_table = """Continent{{tab}}Country{{tab}}Capital
Europe{{tab}}Luxembourg{{tab}}Luxembourg
Asia{{tab}}Thailand{{tab}}Bangkok"""

         // save string to temporary file (error handling code omitted for brevity)
         const file_handle temporary_file = capitals_table.co_store_to_new_temporary_file.o_file_handle
      
         // restore string from temporary file (error handling code omitted for brevity)
         const string restored_capitals_table = fa_string.co_create_from_text_file.o_result ( temporary_file )
         
         // now decompose capitals_table into rows and columns
         repeat for each string row in restored_capitals_table.co_split_by_lines
            se_console.co_message ( row )
            repeat for each string column in row.co_split ( se_string.a_tab )
               se_console.co_message ( column )
            end repeat
            se_console.co_new_line
         end repeat

         // delete temporary file
         c_temporary_file.co_delete_file

      end script
   end command
   
end service

The result on screen is:

Continent       Country Capital
Continent
Country
Capital

Europe  Luxembourg      Luxembourg
Europe
Luxembourg
Luxembourg

Asia    Thailand        Bangkok
Asia
Thailand
Bangkok

See also