multidimensional arrays problem

M

Mrkljus

Hi, ppl

Somebody knows why this works in MSIE6 and not in MSIE7 :

artPage=new Array();
artPage[42]=new Array();
artPage[42][0]=null;
artPage[42][1]=new Array();
artPage[42][1][0]=1290;
artPage[42][1][1]=1291;
artPage[42][2]=new Array();
artPage[42][2][0]=1292;
artPage[42][2][1]=1319;

so there is a 'hole' between 0 and 42, those elements are not initialized.
MSIE7 reads that there are 40 empty elements in first dimension. MSIE6 has
no problems with those variables at all so there might be some 'security
issue' which imposed MSIE7 is ignoring such code, I do not know now how to
handle this situation..

Any help?

Thanks
 
E

Erwin Moller

Mrkljus said:
Hi, ppl

Somebody knows why this works in MSIE6 and not in MSIE7 :

artPage=new Array();
artPage[42]=new Array();
artPage[42][0]=null;
artPage[42][1]=new Array();
artPage[42][1][0]=1290;
artPage[42][1][1]=1291;
artPage[42][2]=new Array();
artPage[42][2][0]=1292;
artPage[42][2][1]=1319;

so there is a 'hole' between 0 and 42, those elements are not initialized.
MSIE7 reads that there are 40 empty elements in first dimension. MSIE6 has
no problems with those variables at all so there might be some 'security
issue' which imposed MSIE7 is ignoring such code, I do not know now how to
handle this situation..

Any help?

Thanks

Hi,

[from Javascript, the definitive guide, 4th edition]
Arrays in js may be 'sparse'. Then means that array indexes need not fall
into a contiguous range of numbers: a javascript implementation may
allocate memory only for those array elemets that are actually stored in
the array.

I always thought they are always sparse.
I use a browser, so I don't have access to IE7 at the moment, but are you
sure MSIE7 creates all the slots (from 0 to 41)?
How did you test?
What was the result?
me curious.

Regards,
Erwin Moller
 
M

Mrkljus

there are 40 elements in first dimension, but those are not all one behind
another: 0,1,2,3 ... it is put into array in order: 42, 98, 114, 589 ...

so there are many 'holes' in between elements. Since I do not know how
should I create something like java's Hashtable it was the easiest solution
which worked untill Microsoft issued MSIE7.

Maybe someone knows where can I download implementation of something like
Hashtable in javascript? Should I fill area between 0 and 42 with nulls?

"Erwin Moller"
Mrkljus said:
Hi, ppl

Somebody knows why this works in MSIE6 and not in MSIE7 :

artPage=new Array();
artPage[42]=new Array();
artPage[42][0]=null;
artPage[42][1]=new Array();
artPage[42][1][0]=1290;
artPage[42][1][1]=1291;
artPage[42][2]=new Array();
artPage[42][2][0]=1292;
artPage[42][2][1]=1319;

so there is a 'hole' between 0 and 42, those elements are not
initialized.
MSIE7 reads that there are 40 empty elements in first dimension. MSIE6
has
no problems with those variables at all so there might be some 'security
issue' which imposed MSIE7 is ignoring such code, I do not know now how
to
handle this situation..

Any help?

Thanks

Hi,

[from Javascript, the definitive guide, 4th edition]
Arrays in js may be 'sparse'. Then means that array indexes need not fall
into a contiguous range of numbers: a javascript implementation may
allocate memory only for those array elemets that are actually stored in
the array.

I always thought they are always sparse.
I use a browser, so I don't have access to IE7 at the moment, but are you
sure MSIE7 creates all the slots (from 0 to 41)?
How did you test?
What was the result?
me curious.

Regards,
Erwin Moller
 
P

purcaholic

Hi, ppl

Somebody knows why this works in MSIE6 and not in MSIE7 :

artPage=new Array();
artPage[42]=new Array();
artPage[42][0]=null;
artPage[42][1]=new Array();
artPage[42][1][0]=1290;
artPage[42][1][1]=1291;
artPage[42][2]=new Array();
artPage[42][2][0]=1292;
artPage[42][2][1]=1319;

so there is a 'hole' between 0 and 42, those elements are not initialized.
MSIE7 reads that there are 40 empty elements in first dimension. MSIE6 has
no problems with those variables at all so there might be some 'security
issue' which imposed MSIE7 is ignoring such code, I do not know now how to
handle this situation..

Any help?

Thanks

Hi Mrkljus,

maybe you tried something like:
[snip]
for (i=0; i < artPage.length; i++) {
document.write("pos: "+i+"<br>");
}
[/snap]
Output will be 43 lines (Tested in IE7, Opera9 and FF2).

Iterate the array as follows:
[snip]
for (i in artPage) {
document.write("key: "+i+"<br>");
// access to artPage[1][0] should return 1290
}
[/snap]
You will see, the output is only 42 (Tested in IE7, Opera9 and FF2).


purcaholic
 
E

Erwin Moller

Mrkljus said:
there are 40 elements in first dimension, but those are not all one behind
another: 0,1,2,3 ... it is put into array in order: 42, 98, 114, 589 ...

so there are many 'holes' in between elements. Since I do not know how
should I create something like java's Hashtable it was the easiest
solution which worked untill Microsoft issued MSIE7.

Maybe someone knows where can I download implementation of something like
Hashtable in javascript? Should I fill area between 0 and 42 with nulls?

Consider using an Object.
I am not sure if this helps in your situation, but you can mimic hashing
like this in javascript:

var myHash = new Object();
myHash["element3"] = 'testing';
myHash["element4"] = [1,5,78];
myHash["mrkjlus"] = "yes.";
myHash["elem67"] = 3.14;
etc.

If you need to walk over the values:
for(aKey in myHash){
// aKey is "element3" "element4" etc
// get the val:
alert(aKey+" contains "+myHash[aKey]);
}

Does that help?

Regards,
Erwin Moller
"Erwin Moller"
Mrkljus said:
Hi, ppl

Somebody knows why this works in MSIE6 and not in MSIE7 :

artPage=new Array();
artPage[42]=new Array();
artPage[42][0]=null;
artPage[42][1]=new Array();
artPage[42][1][0]=1290;
artPage[42][1][1]=1291;
artPage[42][2]=new Array();
artPage[42][2][0]=1292;
artPage[42][2][1]=1319;

so there is a 'hole' between 0 and 42, those elements are not
initialized.
MSIE7 reads that there are 40 empty elements in first dimension. MSIE6
has
no problems with those variables at all so there might be some 'security
issue' which imposed MSIE7 is ignoring such code, I do not know now how
to
handle this situation..

Any help?

Thanks

Hi,

[from Javascript, the definitive guide, 4th edition]
Arrays in js may be 'sparse'. Then means that array indexes need not fall
into a contiguous range of numbers: a javascript implementation may
allocate memory only for those array elemets that are actually stored in
the array.

I always thought they are always sparse.
I use a browser, so I don't have access to IE7 at the moment, but are you
sure MSIE7 creates all the slots (from 0 to 41)?
How did you test?
What was the result?
me curious.

Regards,
Erwin Moller
 
R

RobG

Hi, ppl

Somebody knows why this works in MSIE6 and not in MSIE7 :

artPage=new Array();
artPage[42]=new Array();
artPage[42][0]=null;
artPage[42][1]=new Array();
artPage[42][1][0]=1290;
artPage[42][1][1]=1291;
artPage[42][2]=new Array();
artPage[42][2][0]=1292;
artPage[42][2][1]=1319;

so there is a 'hole' between 0 and 42, those elements are not initialized.
MSIE7 reads that there are 40 empty elements in first dimension.

There should be 42 "empty" elements (0 to 41 inclusive), the length
should be 43 (the largest index plus one). Both Firefox and IE 7 show
length = 43.

MSIE6 has
no problems with those variables at all so there might be some 'security
issue' which imposed MSIE7 is ignoring such code, I do not know now how to
handle this situation.

What situation? If you iterate over the array using a for loop like:

for (var i=0, len=artPage.length; i<len; i++){
/* do stuff with artPage */
}

then browsers will iterate over all the elements from index 0 to 42.
If you use a for..in loop:

for (var idx in artPage) {
/* do stuff with artPage[idx] */
}

then browsers will iterate over only those properties that have been
declared (i.e. they will go straight to artPage[42]). There might be
some exceptions, but I expect they aren't the mainstream browsers.

Be careful of libraries that extend built-in objects like Array, a
for..in loop will include any properties that have been added to
Array.prototype.
 
D

dd

Maybe someone knows where can I download
implementation of something like Hashtable

Not sure if this would meet your needs,
but have you considered converting your
non-sequential numbers into strings?

artPage=[];
artPage["_"+42]=[];
artPage["_"+42][0]=null;
etc...

alert(artPage["_"+42].length);

Another separate idea, how about an extra
array used purely for a lookup:

var idxs=[42,98,114,589];
IX=function(n){
for(var i=0;i<idxs.length;i++)
if(idxs==n)
return idxs;
return -1;
}

artPage=[];
artPage[IX(42)]=[];
etc... (obviously you need to handle
-1 being returned, or throw an exception,
etc...)
 
M

Mrklju¹

I think I've found solution to problem: If you write

artPage=new Array(); in one script file, and fill data in another it won't
work... cannot be sure of this, but when I put initialization in the same
file where data lies, everything is functioning!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top