More on dereferencing: how to copy content into a new variable?

  • In short, my question is: how to make a new variable a and give it the same content as an existing object?

    Let's say I have the following:

    Code
    <enum name="floors">
    	<item name="floor_1" data="mydata" />
    	<item name="floor_2" data="mydata" />
    	<item name="floor_3" data="mydata" />
        </enum>

    So I can do:

    Code
    trace(enum[floors].item[0].name, " has as data: ", enum[floors].item[0].data);

    Now I wish to make a new variable a which should get the same contents as my first item node above, so that "a" is the equivalent of "enum[floors].item[0]", and I could do things like:

    Code
    trace(a.name, " has as data: ", a.data);


    Is there any way to accomplish this? I tried:

    Code
    copy(a, enum[floors].item[0]);


    This will make a.name available, but not a.data.

    Then I tried adding:

    Code
    copy(a.content, enum[floors].item[0].content);


    but it doesn't help, still no a.data... And if I only do this copy by itself, without the copy above, then even a.name is null...

    Can anyone shed some light on this? Thanks!

    The reason I'm asking this, is to make it part of a foreach action where one can straight away give the commands one wants to execute on the object's nodes, instead of having to refer to yet another action. But then of course one needs to pass a name for the temporary object one wants to use in the command that needs to be iterated. And that's where the trouble above started...

  • Hi Ronny,

    Zitat

    Is there any way to accomplish this? I tried:

    Code
    copy(a, enum[floors].item[0]);

    This will make a.name available, but not a.data.

    I have tried this:

    Code
    trace(enum[floors].item[0].name, " has as data: ", enum[floors].item[0].data);
    copy(a, enum[floors].item[0]);
    trace(a.name, " has as data: ", a.data );


    and for me it works...

    SAlut.

  • Hmmm.... You are right! *confused*

    After some more investigation, I figured that in my attempts to simplify my problem so that I could post it here without scaring everyone away, I also eliminated the bug that I obviously made in my real application.

    Anyway, now that my problem is solved, I proudly present here a sibling of Steve's foreach() function. In this version, it is not required to pass the name of an action, but one can write the actual command string instead. Note that Steve's version will execute faster, as it does not need to copy around data internally. But mine also has a right to exist. *wink*

    Now, having the same data as above, one can loop through them as follows:

    Code
    <action name="test">
    	foreach(enum[floors].item, i, a, trace("#", i, " is ", a.name, " and has as data: ", a.data ); );
    </action>

    The output is:
    INFO: #0 is floor_1 and has as data: mydata
    INFO: #1 is floor_2 and has as data: mydata
    INFO: #2 is floor_3 and has as data: mydata

    PS. Of course one can specify more than one command, as usual, by separating them with semicolons ";".

    - Ronny D'Hoore

    Einmal editiert, zuletzt von rdhoore108 (12. September 2010 um 14:30)

  • There are two issues with this actually:

    1. if you change the value of any items that are part of the temporary object (a in this example), then of course these changes will not reflect back to the original object. It would come handy if one could assign variables by reference instead of by value, so that a and enum[floors].item[get(i)] would both refer to the same structure in memory.
    2. get(i) should have been get(%2).

    Below is the corrected code that fixes these issues, and was also optimized in a few ways.

    - Ronny D'Hoore

    3 Mal editiert, zuletzt von rdhoore108 (18. September 2010 um 10:07)

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!