A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Array push not working??

  1. #1
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929

    Array push not working??

    I've got the following code loading some xml, creating dynamic arrays and pushing the data into the array...

    I pulled some pieces out because it seems like each push overwrites the previous entry and I'm not sure why it's not working?! I posted the xml and code (without the trace statements) below, and also attached the fla and xml files in a zip...if anyone has an idea what I've got wrong that would be GREAT!!!


    Code:
    var counties:XML = new XML();
    counties.ignoreWhite = true;
    counties.onLoad = function(success) {
    	xN = counties.firstChild.firstChild;
    	do {
    		currID = xN.attributes.ID;
    		var countyID:Array = new Array();
    		_root["arr"+currID] = countyID;
    		loadLocs();
    		xN = xN.nextSibling;
    	} while (xN.firstChild != null);
    };
    // Load the XML
    counties.load("locsName.xml");
    loadLocs = function () {
    	nextN = xN.firstChild.firstChild;
    	var tmp:Object = new Object();
    	do {
    		tmp.Name1 = nextN.childNodes[0].childNodes[0];		
    		_root["arr"+currID].push(tmp);
    		nextN = nextN.nextSibling;
    	} while (nextN.firstChild != null);
    };
    and the xml:

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <Counties>	
    	<County Name="Cook" ID="031" eRegion="1">
    		<Locs>
    			<Loc>
    				<Name1>Air Cycle Corporation</Name1>				
    			</Loc>
    			<Loc>
    				<Name1>Assistive Technology Exchange Network</Name1>			
    			</Loc>
    			<Loc>
    				<Name1>Chicago Coalition for Information Access Community</Name1>
    			</Loc>				
    		</Locs>
    	</County>
    	<County Name="Crawford" ID="033" eRegion="8">
    		<Locs>
    			<Loc>
    				<Name1>Crawford test</Name1>				
    			</Loc>
    		</Locs>
    	</County>
    	<County Name="DuPage" ID="043" eRegion="1">
    		<Locs>
    			<Loc>
    				<Name1>Com2 Computers and Technology</Name1>
    			</Loc>
    			<Loc>
    				<Name1>Fortune Plastic and Metal, Inc.</Name1> 
    			</Loc>
    		</Locs>
    	</County>	
    <Counties>
    Thanks!!
    Last edited by flashpipe1; 09-10-2007 at 01:37 PM.
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Try initializing the tmp variable inside the do, while loop...
    Code:
    counties.load("locsName.xml");
    loadLocs = function () {
    	nextN = xN.firstChild.firstChild;
    	do {
    		var tmp:Object = new Object();
    		tmp.Name1 = nextN.childNodes[0].childNodes[0];
    		_root["arr" + currID].push(tmp);
    		nextN = nextN.nextSibling;
    	} while (nextN.firstChild != null);
    };
    PS. Did you get the file I emailed to you last night?

  3. #3
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    Yeah, unfortunately, that clears the array out each time (since it creates a new array each time it goes through the loop) and I only end up with the final value instead of each value...

    And, yes, I did get the file...THANKS!! I replied to your email and forwarded it on to my client...haven't heard from him yet, which is usually a good thing...

    Thanks!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Okay.
    This is what I tried...
    Code:
    var counties:XML = new XML();
    counties.ignoreWhite = true;
    counties.onLoad = function(success) {
    	xN = counties.firstChild.firstChild;
    	do {
    		currID = xN.attributes.ID;
    		var countyID:Array = new Array();
    		_root["arr" + currID] = countyID;
    		loadLocs();
    		xN = xN.nextSibling;
    	} while (xN.firstChild != null);
    	trace(" ---------------- ")
    	for ( i in _root["arr031"]){
    		trace(i + " " + _root["arr031"][i])
    		for ( j in _root["arr031"][i]){
    			trace(j + " " + _root["arr031"][i][j])
    		}
    	}
    	trace(" ---------------- ")
    	trace(_root["arr031"][0].Name1)
    };
    // Load the XML
    counties.load("locsName.xml");
    loadLocs = function () {
    	nextN = xN.firstChild.firstChild;
    	do {
    		var tmp:Object = new Object();
    		tmp.Name1 = nextN.childNodes[0].childNodes[0];
    		_root["arr" + currID].push(tmp);
    		nextN = nextN.nextSibling;
    	} while (nextN.firstChild != null);
    };
    And it traces out the values, weird?

  5. #5
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    ah, in the first do loop...cool...let me try that...
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  6. #6
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    SWEET!! That did it!! Thank you!! I think the key was to declare the object within the second do...while.

    Whew! I thought I was going to have to pull all my hair out...



    Thanks!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

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