Why not also "foreach"

  • Hi there

    I've implemented a "foreach" function which allows you to iterate, for instance, over all hotspots, plugins, or elements in an XML structure.

    Since the new prerelase has the fantastic "for" and "loop", why not also a "foreach"? I've found it very useful on my projects.

    Here's my implementation and a test case:
    <!-- %1 - what to iterate
    %2 - iterator variable
    %3 - variable where iterated structure will be stored
    %4 - action -->
    <action name="foreach">
    set(%2,0);
    loop(%1[get(%2)] !== null,
    copy(%3, %1[get(%2)]);
    %4;
    inc(%2);
    );
    </action>


    <teststruct>
    <thumb name="hello" someproperty="this"/>
    <thumb name="foo" someproperty="is a"/>
    <thumb name="bar" someproperty="test"/>
    </teststruct>

    <action name="test-foreach">
    foreach(teststruct.thumb,tmpvar,loopedstruct,
    trace(tmpvar);
    trace(loopedstruct.name);
    trace(loopedstruct.someproperty);
    )
    </action>

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!