PDA

View Full Version : Multidimensional arrays


stack13
04-02-2005, 11:49 PM
Hi all.Im trying some maps here, made with
multidimensional arrays. Once declared I wanted
to modify them by removing elements first, but I found some
troubles:

map=new Array();
map[1]=new Array();
map[1][1]=new Array();
map[1][1][16]=null;

map[2]=new Array();
map[2][1]=new Array();
map[2][1][17]=null;

map[2][4]=new Array();
map[2][4][12]=null;

map[3]=new Array();
map[3][2]=new Array();
map[3][2][16]=null;
map[3][2][18]=null;
map[3][2][20]=null;
map[3][2][22]=null;
map[3][4]=new Array();
map[3][4][5]=null;
map[3][4][10]=null;

Because my map has three layers I decided to use three
dimensions [value is not used for this propose].
removing elements:

function showArray(text,object) {
document.write('<strong>'+text+'</strong>: ');
document.write('<BR>');
var c=0;
for(var i in object){
if(typeof(object[i]=="object")){
for(var e in object[i]){
if(typeof(object[i][e]=="object")){
for (var a in object[i][e]){
document.write(i+' => '+e+' => '+a+' ');
document.write('<BR>');
c++;
}
}
else {
document.write(i+':'+e+' ');
document.write(' => ');
}
}
}
else {
document.write(i+' ');
document.write(' => ');
}
}
document.write(c+' items<br>---<br>');
}
function remMapElement(array,e){
if(typeof(array[e])=="undefined") return false;
// splits because splice behaviors is different for last dimension arrays
// map[x][x]
if(array[e]!=null){
array.splice(e,1,false);
}
// map[x][x][x]
else{
array.splice(e,1);
array.unshift(0,false);
}
}

showArray('Unsorted',map);
//remMapElement(map,3)
//remMapElement(map[3],4)
remMapElement(map[3][4],5)
showArray('Sorted',map);

As you see, array.splice(e,1,false) works good for 1st,2nd dimension, but I cant find the right method for removing 3rd dimension correctly.
array.splice(e,1) will work for last indexes like remMapElement(map[3][4],10). For non-last indexes like remMapElement(map[3][4],5) it would return 3 => 4 => 9 . Is there any method I could use in this case for removing non-last indexes without shifting array's indexes? Thanks in advance.

--mac

suendisra
04-03-2005, 01:43 AM
Your problem could be rooting out from the new Array function you're trying to implement. When you use new Array(), you must define the number to array.

str = new Array(2);

str[0] = "Hello, ";
str[1] = "welcome ";
str[2] = new Array(2);

str[2][0] = "friends ";
str[2][1] = "one and ";
str[2][2] = "all.";

Or you can map out your arrays using literal notation.

str = ["Hello, ", "welcome ", ["friends ", "one and ", "all."]];

:thumbsup: :thumbsup:

Harry Armadillo
04-03-2005, 07:19 AM
It would help to have a better explanation of what you are trying to accomplish and how. Is there a reason you've chosen arrays? Since you seem to only what elements to exist at certain index-combinations, perhaps objects would be better? Why a value of null? Couldn't you use a value to test whether an element is valid instead of simply checking its existance?

In the meantime, here's a goof: if(typeof(object=="object"))

object=="object" will evaluate first, giving the boolean value of false, then typeof will act upon that, giving the string 'boolean', which will be typecast as true for the conditional...

Use: if(typeof(object)=="object")

stack13
04-04-2005, 03:22 AM
You may take a look if you want.
http://www.publicarhoy.com.ar/manager/map.php
which is a manager module of
http://www.publicarhoy.com.ar/
user:mac
pass:mac

Any suggestions will be appreciated.
--mac

Harry Armadillo
04-04-2005, 08:00 AM
Links come back 404. :(

stack13
04-04-2005, 01:35 PM
Sorry. Updated.

Harry Armadillo
04-04-2005, 06:56 PM
That's some serious stuff you've got there.

I feel stupid for not thinking of this earlier:

delete array[i]

completely removes i from array, with out any change any of the other indices. It will however mess up some array properties and methods like length and join, but I don't think that will matter to you.

stack13
04-04-2005, 07:17 PM
Thanks Harry. I'll check it out.