...

View Full Version : Question about Arrays



phenom01
12-11-2011, 09:42 PM
Do arrays retain its data outside of a function. The reason why i asked this is because i created a global array and then a function. In the function i assaigned values to the global array but when i try to access the global array it gives me a "undefined" or "null" error.



var myArray = new Array();
var count = 0;

function populate(){
for(i=0; i < 5; i++){
myArray[i] = count++;
}

}

thesam101
12-11-2011, 10:01 PM
Hi phenom01

Yes, myArray would hold the values. :thumbsup:

Can we see the full code? (the code you are using to access it) As I suspect that is where the problem lies :eek:

P.S. I should also probably mention that I think its technically preferrable to code it like so instead:


function populate(){
for(i=0; i < 5; i++){
myArray[i] = count;
count++;
}

}

phenom01
12-11-2011, 10:53 PM
function test(){
for(i=0; i < 5; i++){
document.write(myArray[i]);
}
}

Old Pedant
12-11-2011, 10:56 PM
Why do you need a separate count variable at all?


var myArray = [];

function populate(){
for(i=0; i < 5; i++){
myArray[i] = i;
}
}

But if you really only have 5 elements and they really are as simple as that, why not do


var myArray = [];

function populate(){
myArray = [0,1,2,3,4];
}

phenom01
12-11-2011, 11:01 PM
because i am trying to see if arrays retain their values outside of a function. They are not retaining values.

Old Pedant
12-11-2011, 11:02 PM
So put it all together. Since you shouldn't be using document.write() to create HTML outside of the <body>, and yet the document.write() must be done before the page is finished loading, you might as well just do it all in the <body>:



<html>
<body>
<script type="text/javascript">
var myArray = [];

function populate(){
for(i=0; i < 5; i++){
myArray[i] = i;
}
}
function test(){
for(i=0; i < 5; i++){
document.write(myArray[i]);
}
}

populate();
test();
</script>
</body>
</html>


But of course that would also work like this:


<html>
<head>
<script type="text/javascript">
var myArray = [];

function populate(){
for(i=0; i < 5; i++){
myArray[i] = i;
}
}
populate(); // must populate the array *before* page is loaded...

// since this function does document.write and that MUST be done before end of page loading:
function test(){
for(i=0; i < 5; i++){
document.write(myArray[i]);
}
}
</script>
</head>
<body>
Here is my test:
<script type="text/javascript">
test();
</script>
</body>
</html>

phenom01
12-11-2011, 11:08 PM
can't i do this on an external js file? What im really trying to do is load an xml file but IE gives me a "access denied" error. It works on firefox though but with firefox i cant use activeXobjet to write to files.

Here is my code to load xml file into array:


function ReadXML() {
xmlDoc = loadMyXML("xmlStudent.xml");
alert(xmlDoc.childNodes.length);

for (x = 0; x < xmlDoc.childNodes.length; x++) {
var tmpPerson = new Array(xmlDoc.getElementsByTagName("studid")[x].childNodes[0].nodeValue,
xmlDoc.getElementsByTagName("FN")[x].childNodes[0].nodeValue,
xmlDoc.getElementsByTagName("LN")[x].childNodes[0].nodeValue);

document.write(xmlDoc.getElementsByTagName("studid")[x].childNodes[0].nodeValue + ", " + xmlDoc.getElementsByTagName("FN")[x].childNodes[0].nodeValue + ", " + xmlDoc.getElementsByTagName("LN")[x].childNodes[0].nodeValue + "<br />");
}

Old Pedant
12-11-2011, 11:13 PM
Let's go move to your other thread, where you are trying to read the XML. That's where the real problem lies.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum