dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Need help with syntax of dynamic variable

  1. #1
    Senior Member rob5150's Avatar
    Join Date
    Nov 2000
    Posts
    150

    Need help with syntax of dynamic variable

    This is totally throwing me. My original variables are commented at the bottom. I am trying to convert those into dynamically created variables within the for loop. How do I write the syntax of this? When I test in Flash it comes up as undefined.

    for(var i=0;i < oItems_xml.length;i++) {
    this["oItem"+i+"Frames_xml"] = oItems_xml[i].childNodes;
    this["oItem"+i+"Frame_xml" = oItems_xml[i].firstChild;
    //trace(oItems_xml[i].childNodes);
    //trace(oItems_xml[i].firstChild);
    }


    //oItem1Frames_xml = oItems_xml[0].childNodes;
    //oItem1Frame_xml = oItems_xml[0].firstChild;

  2. #2
    Flash Droid
    Join Date
    Jan 2002
    Location
    Birmingham, England.
    Posts
    180
    Try this:-

    for(var i=0;i < oItems_xml.length;i++) {
    eval("oItem"+i+"Frames_xml") = oItems_xml[i].childNodes;
    eval("oItem"+i+"Frame_xml") = oItems_xml[i].firstChild;
    //trace(oItems_xml[i].childNodes);
    //trace(oItems_xml[i].firstChild);
    }

    or alternatively you have a missing "]" on the third line of your script.

  3. #3
    Senior Member
    Join Date
    Aug 2001
    Location
    Egypt
    Posts
    332
    just a notice.. if you're using flash mx, you can't use
    eval("oItem"+i+"Frames_xml") = oItems_xml[i].childNodes;
    you must use instead
    this["oItem"+i+"Frames_xml"] = oItems_xml[i].childNodes;
    you'll get this error:
    Left side of assignment operator must be variable or property.

    cheers.
    Hamza.

  4. #4
    Senior Member rob5150's Avatar
    Join Date
    Nov 2000
    Posts
    150
    Neither work. The eval() function does throw that error. Yeah I forgot to put that last ] in the second line but that wasn't the problem. Here is what I have now and it doesn't work. When I trace those variables in any capacity I get nothing. Even when I put in a string as the value, like = "blah".

    for(var i=0;i < oItems_xml.length;i++) {
    //trace(i);
    this["oItem"+i+"Frames_xml"] = oItems_xml[i].childNodes;
    this["oItem"+i+"Frame_xml"] = oItems_xml[i].firstChild;
    //trace(oItems_xml[i].childNodes);
    //trace(oItems_xml[i].firstChild);
    }

    //oItem1Frames_xml = oItems_xml[0].childNodes;
    //oItem1Frame_xml = oItems_xml[0].firstChild;
    trace("Total Frames : " + oItem0Frames_xml.length); (this was wrong but I corrected and it's still not giving me the correct length.)
    //trace(oItem1Frames_xml[0].nodeName);
    Last edited by rob5150; 02-26-2003 at 10:56 AM.

  5. #5
    Senior Member
    Join Date
    Aug 2001
    Location
    Egypt
    Posts
    332
    hi,
    can you please tell me what do you get when testing this:

    Code:
    for(var i=0; i<oItems_xml.length; i++) {
        trace(oItems_xml[i].childNodes); 
        trace(oItems_xml[i].firstChild);
    }
    and also test this one, and then open the List Variables window, and see if all the variables called oItem0Frames_xml, oItem1Frames_xml,... are equal to "test":

    Code:
    for(var i=0; i<oItems_xml.length; i++) {
        this["oItem"+i+"Frames_xml"] = "test"; 
    }
    waiting for your reply...

    cheers,
    Hamza.

  6. #6
    Senior Member rob5150's Avatar
    Join Date
    Nov 2000
    Posts
    150
    You are right. It is assigning the value to the variable. I am having difficulty tracing the .length of it though. Which is the whole point of what I am trying to do.

    If I do this within that loop it outputs all of the XML.

    this["oItem"+i+"Frames_xml"] = oItems_xml[i].childNodes;
    this["oItem"+i+"Frame_xml"] = oItems_xml[i].firstChild;
    trace(this["oItem"+i+"Frames_xml"]);
    trace(this["oItem"+i+"Frame_xml"]);

    This the trace for length.
    trace("Total Frames : " + this["oItem"+i+"Frames_xml"].length);

    Perhaps I need to just create another variable for length.

  7. #7
    Senior Member rob5150's Avatar
    Join Date
    Nov 2000
    Posts
    150
    I think I got it. Not sure what was happening in my trace. I redid it and now it work. This XML stuff IS REALLY complicated. One little baby step at a time. I am such a newbie at this. Thanks for the help. I thought I was writing those variables right.

  8. #8
    Senior Member rob5150's Avatar
    Join Date
    Nov 2000
    Posts
    150
    Okay I'm back. I restructured my XML doc and hit the drawing boards again. Should be a little easier now. I have ditched the whole for loop process of generating variables for the XML data. Hadn't realized up until now I could directly call the data from the main XML variable. So I am stuck there now with only one dynamic part of the variable holding me back. If you help me with this you are no doubt to get some good karma out of this.

    Here is the wopper of a string I am having problems with:
    trace("SubTitle = "+_level0.oContent_xml.firstChild.childNodes[(_root.productmovie._currentframe-1)].childNodes[k].attributes.SubTitle);

    The error is occuring with the "this.k" or if I use "k" variable.

    "k" is defined because I can trace that. BUT it's always set to the last number in my series of loops. Meaning, the loop that generates all this runs 4 times for 4 items. There are 4 items in the XML doc but of course you have to reference them 0-3. If I change it to (k-1) I get the last item in the XML doc. So it works, but only always shows the last record. Hmmmmmm. Not sure how to fix this.

    If I directly call an item of the XML doc it can read the data. Example: trace("SubTitle = "+_level0.oContent_xml.firstChild.childNodes[(_root.productmovie._currentframe-1)].childNodes[0].attributes.SubTitle);

    What am I doing wrong here?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center