.pop() Method on Global Array
How can I pop() or shift() a globally declared array in a function? I get no error, but it also does nothing. I need to access this array from multiple functions, so I've declared it globally. From within other functions I can look up values in the array, and I can traverse the entire array as a collection with a for(...) loop. .length isn't returning a correct value either.
Thanks,
Andrew
Here's a sample:
Here's the javascript:
inlets=1;
outlets=1;
autowatch=1;
var bars = new Array();
var RFID = new String;
function bang() {
post("***n");
for(var bar in bars) {
post("RF: " + bar + ", Length: " + bars[bar] + "n");
}
post("***n");
}
function NewRF(rf,l) {
RFID=rf;
bars[rf]=l;
bang();
}
function GetBar(rf) {
l=bars[rf];
post("Length: " + l + "n");
}
function Remove() {
n=bars.pop();
post("Removing: " + n + "n");
bang();
}
It seems that pop only pops array elements that have been pushed. Splice(), likewise only works on elements that have been pushed. If you intersperse pushed and 'regularly' assigned elements, pop and splice will act only on those pushed skipping over the others. Insert the line: bars.push(l); before or after: bars[rf]=l; in function NewRF() to see.
Is this a bug or is this in the Javascript spec? The example here: http://www.w3schools.com/jsref/jsref_splice.asp , while for a browser, doesn't require the elements to've been pushed first. I found another example where pop doesn't require pushing. I haven't found any examples of pop in Max.
Thanks,
Andrew
The .length property only reports pushed elements as well. Is this by design?