"Sputnik" help  
Sputnik Help
ForEach, EndE

Description

The foreach directive facilitates iteration over a sequence or collection, allowing you to process each element individually. It supports extensions to provide additional control over the iteration process.

Varients

None.

Parameters

<current>

Variable to represent the current value in each iteration.

<value> The sequence or collection to iterate over.
[from <from>] Optional. Specifies the position to start from in the sequence (<value>).
[by <by>] Optional. Specifies how many values to skip per iteration. Default is 1.
[ref] Optional. Indicates that the iteration variable <current> should be treated as a reference.

Remarks

The foreach directive iterates over each element in the specified sequence (<value>), assigning the current element to the variable <current> in each iteration. Extensions such as from, by, and ref provide additional control and flexibility for customizing the iteration process.

To learn more go the main preprocessor macro section.

Related

Example (#)

Example of usage:

#eq PAX, "cat" "dog" "fox" 777
#foreach PCX in PAX
    say PCX;
#ende
    
// PRINTS
// cat
// dog
// fox
// 777

 

Example (.)

Example of usage:

#macro foo()
    .pushap
    
    // Example 1: Numerical range from 100 to 800 with a step of 100
    .eq PAX, 100 200 300 400 500 600 700 800
    .foreach PCX in PAX
        say PCX;
    .ende
    
    // Example 2: String range with a numerical value
    .eq PAX, "cat" "dog" "fox" 777
    .foreach PCX in PAX
        say PCX;
    .ende
    
    .popap
#endm

foo();

// PRINTS
// 100
// 200
// 300
// 400
// 500
// 600
// 700
// 800
// cat
// dog
// fox
// 777

Using the by:

#macro foo()
    .pushap
    
    .eq PAX, 100 200 300 400 500 600 700 800
    .foreach PCX in PAX by 2
        say PCX;
    .ende
    
    .popap
#endm

foo();

// PRINTS
// 100
// 300
// 500
// 700

Using the from:

#macro foo()
    .pushap
    
    .eq PAX, 100 200 300 400 500 600 700 800
    .foreach PCX in PAX from 2
        say PCX;
    .ende
    
    .popap
#endm

foo();

// PRINTS
// 300
// 400
// 500
// 600
// 700
// 800

Using the from and the by:

#macro foo()
    .pushap
    
    // Example 1: Numerical range from 100 to 800 with a step of 100
    .eq PAX, 100 200 300 400 500 600 700 800
    .foreach PCX in PAX from 2 by 2
        say PCX;
    .ende
    
    .popap
#endm

foo();

// PRINTS
// 300
// 500
// 700

Using the ref:

#macro bar()
    .pushap
    
    say "Without ref";
    .eq PAX, 100 200 300 400 500 600 700 800
    .foreach PCX in PAX
        .add PCX, 2
        say PCX;
    .ende
    
    say "After no ref";
    .foreach PCX in PAX
        say PCX;
    .ende
    
    .popap
#endm

#macro foo()
    .pushap
    
    say "With ref";
    .eq PAX, 100 200 300 400 500 600 700 800
    .foreach PCX in PAX ref
        .add PCX, 2
        say PCX;
    .ende
    
    say "After ref";
    .foreach PCX in PAX
        say PCX;
    .ende
    
    .popap
#endm

bar();
foo();

// PRINTS
// Without ref
// 102
// 202
// 302
// 402
// 502
// 602
// 702
// 802
// After no ref
// 100
// 200
// 300
// 400
// 500
// 600
// 700
// 800
// With ref
// 102
// 202
// 302
// 402
// 502
// 602
// 702
// 802
// After ref
// 102
// 202
// 302
// 402
// 502
// 602
// 702
// 802

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org