Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't access Global array outside of For loop.

Status
Not open for further replies.

sarcus25

Programmer
Apr 14, 2005
27
0
0
Hi, I'm trying to create a global array that will hold a list of values that it retrives from an xml file. The global array values can only be traced inside the for loop. I am not able to access the array outside of the for loop. Here is the code used. Thanks in advance.

Code:
//initial values
//==============
_global.songName = new Array();
_global.songAdded = new Array();
aSwitch = true;


//Striping the xml data and putting it into arrays
//==================================================
function XMLOut(xml_file):Void{
	for (var i=0;i < xml_file.firstChild.childNodes.length; i++){
		
		for (var j=0; j < xml_file.firstChild.firstChild.childNodes.length;j++){
			
			if (aSwitch){
		    _global.songName.push(xml_file.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue);
			aSwitch = !aSwitch;
			trace("inside for loop SongName: " + i + " " + _global.songName[i]);]);//Returns the Song Name
			}
          else{
_global.songAdded.push(xml_file.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue);
				aSwitch = !aSwitch;
			trace("inside for loop SongAdded: " + i + " " + _global.songAdded[i]);//Returns the Date the song was added
			}
		}
		
		
	}
}



trace(_global.songName[0]);//Displays undefined
trace(_global.songAdded[0]);//Displays undefined
 
are you running the trace before array has been populated?

try adding the trace statements into the function at the end of the for loop

Code:
function XMLOut(xml_file):Void{
    for (var i=0;i < xml_file.firstChild.childNodes.length; i++){
        
        for (var j=0; j < xml_file.firstChild.firstChild.childNodes.length;j++){
            
            if (aSwitch){
            _global.songName.push(xml_file.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue);
            aSwitch = !aSwitch;
            trace("inside for loop SongName: " + i + " " + _global.songName[i]);]);//Returns the Song Name
            }
          else{
_global.songAdded.push(xml_file.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue);
                aSwitch = !aSwitch;
            trace("inside for loop SongAdded: " + i + " " + _global.songAdded[i]);//Returns the Date the song was added
            }
        }
        
        
    }
trace(_global.songName[0])
trace(_global.songAdded[0])
}
 
The trace statements are outside of the loop. The idea here is to have access to these arrays from anywhere in my code not just in the for loop. This doesn't seem to be working though. If I put the trace statements inside of the for statement like you mentioned and reference them using array access notation I can see that the elements have something in them. But as soon as they leave the scope of the for loops the array is empty.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top