You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Andreas Neumann <ne...@karto.baug.ethz.ch> on 2003/10/18 07:29:43 UTC

Strange behaviour of Rhino with associative array object

It's again me - 
sorry if I am bombarding you with too many questions ...

-------

I use an associative array object.

ethnicMix["Whites"] =
parseInt(myPoly.getAttributeNS("http://www.carto.net/attrib","WHITE"));
ethnicMix["Blacks"] =
parseInt(myPoly.getAttributeNS("http://www.carto.net/attrib","BLACK"));
ethnicMix["Asians"] =
parseInt(myPoly.getAttributeNS("http://www.carto.net/attrib","ASIAN"));
ethnicMix["Others"] = totalPop2000 - (ethnicMix["Whites"] + ethnicMix["Blacks"]
+ ethnicMix["Asians"]);

Later on I use a "for ... in" loop to extract the values of the associative
array. It appears that the Rhino js engine automatically sorts alphabetically
according to the index value, which means, that "Asians" come first, then "
Blacks", ....

In the other SVG viewers the array wouldn't be sorted, but left in the order I
used originally, which I suppose is the right behaviour.

Or am I wrong - is this probably a but in the Rhino js engine, or did I do
something wrong?

----

to illustrate my problem see http://www.carto.net/geog234/lab3/lab3_end.svg and
compare the behaviour in Adobe or Corel SVG viewer against Batik. The stacked
barchart is messed up in Batik, because of the re-ordering.

Thanks for any infos on this topic,

Andreas

--     
The only thing that stands between a man and what he wants from
life is often merely the will to try it and the faith to believe
that it is possible ...
-- M. DeVos
    
----------------------------------------------     
Andreas Neumann - Department of Cartography     
Swiss Federal Institute of Technology (ETH)     
ETH Hoenggerberg, CH-8093  Zurich, Switzerland     
Phone: ++41-1-633 3031, Fax: ++41-1-633 1153     
e-mail: neumann@karto.baug.ethz.ch     
www: http://www.karto.ethz.ch/neumann/     
SVG.Open/Carto.net: http://www.svgopen.org/     

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Strange behaviour of Rhino with associative array object

Posted by Andreas Neumann <ne...@karto.baug.ethz.ch>.
thank you very much for that hint - yes, not very elegant, but it works ...

Andreas


Zitiere Jim Ley <ji...@jibbering.com>:

> 
> "Andreas Neumann" <ne...@karto.baug.ethz.ch> wrote in message
> news:1066499759.3f917eaf58e7f@karmail.ethz.ch...
> > > what you can do is:
> > >
> > > a=[]
> > > a['chicken']=whatever
> > > a.push(a['chicken'])
> > >
> > > for (i=0;i<a.length;i++) alert(a)
> >
> > ok, got that - works fine so far. But is there a way, while using
> your
> above
> > described for-loop to get at the same time the key of the associative
> array
> > back, that is corresponding with the value?
> 
> No, you'll have to create objects containing the name value
> 
> a=[]
> a['chicken']={key:'chicken',value:whatever}
> a.push(a['chicken'])
> 
> Then access them with a[0].key / a[0].value - but of course that's
> duplicating stuff.
> 
>  var fruits = new Array();
>  fruits["oranges"] = {key:'oranges',value:25};
>  fruits.push(fruits["oranges"]);
>  fruits["bananas"] = {key:'bananas',value:15};
>  fruits.push(fruits["bananas"]);
>  fruits["pineapples"] = {key:'pineapples',value:8};
>  fruits.push(fruits["pineapples"]);
> 
>  for (i=0;i<fruits.length;i++) {
>     alert(fruits[i].key+','+fruits[i].value);
>  }
> 
> Which is pretty nasty...
> 
> Jim.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 
> 



--     
The only thing that stands between a man and what he wants from
life is often merely the will to try it and the faith to believe
that it is possible ...
-- M. DeVos
    
----------------------------------------------     
Andreas Neumann - Department of Cartography     
Swiss Federal Institute of Technology (ETH)     
ETH Hoenggerberg, CH-8093  Zurich, Switzerland     
Phone: ++41-1-633 3031, Fax: ++41-1-633 1153     
e-mail: neumann@karto.baug.ethz.ch     
www: http://www.karto.ethz.ch/neumann/     
SVG.Open/Carto.net: http://www.svgopen.org/     

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Strange behaviour of Rhino with associative array object

Posted by Jim Ley <ji...@jibbering.com>.
"Andreas Neumann" <ne...@karto.baug.ethz.ch> wrote in message
news:1066499759.3f917eaf58e7f@karmail.ethz.ch...
> > what you can do is:
> >
> > a=[]
> > a['chicken']=whatever
> > a.push(a['chicken'])
> >
> > for (i=0;i<a.length;i++) alert(a)
>
> ok, got that - works fine so far. But is there a way, while using your
above
> described for-loop to get at the same time the key of the associative
array
> back, that is corresponding with the value?

No, you'll have to create objects containing the name value

a=[]
a['chicken']={key:'chicken',value:whatever}
a.push(a['chicken'])

Then access them with a[0].key / a[0].value - but of course that's
duplicating stuff.

 var fruits = new Array();
 fruits["oranges"] = {key:'oranges',value:25};
 fruits.push(fruits["oranges"]);
 fruits["bananas"] = {key:'bananas',value:15};
 fruits.push(fruits["bananas"]);
 fruits["pineapples"] = {key:'pineapples',value:8};
 fruits.push(fruits["pineapples"]);

 for (i=0;i<fruits.length;i++) {
    alert(fruits[i].key+','+fruits[i].value);
 }

Which is pretty nasty...

Jim.




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Strange behaviour of Rhino with associative array object

Posted by Andreas Neumann <ne...@karto.baug.ethz.ch>.
> what you can do is:
> 
> a=[]
> a['chicken']=whatever
> a.push(a['chicken'])
> 
> for (i=0;i<a.length;i++) alert(a)

ok, got that - works fine so far. But is there a way, while using your above
described for-loop to get at the same time the key of the associative array
back, that is corresponding with the value?

f.e. if I use:

var fruits = new Array();
fruits["oranges"] = 25;
fruits.push(fruits["oranges"]);
fruits["bananas"] = 15;
fruits.push(fruits["bananas"]);
fruits["pineapples"] = 8;
fruits.push(fruits["pineapples"]);

for (i=0;i<fruits.length;i++) {
   alert(fruits[i]);
}

would give me the value of the fruits - how can I get back the key in the same
loop, to get the fruit to which the values is corresponding to.

Sorry to bother you again, I am still a beginner in Javascript.

Andreas


--     
The only thing that stands between a man and what he wants from
life is often merely the will to try it and the faith to believe
that it is possible ...
-- M. DeVos
    
----------------------------------------------     
Andreas Neumann - Department of Cartography     
Swiss Federal Institute of Technology (ETH)     
ETH Hoenggerberg, CH-8093  Zurich, Switzerland     
Phone: ++41-1-633 3031, Fax: ++41-1-633 1153     
e-mail: neumann@karto.baug.ethz.ch     
www: http://www.karto.ethz.ch/neumann/     
SVG.Open/Carto.net: http://www.svgopen.org/     

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Strange behaviour of Rhino with associative array object

Posted by Jim Ley <ji...@jibbering.com>.
"Andreas Neumann" <ne...@karto.baug.ethz.ch> wrote in message
news:1066491160.3f915d184cb1d@karmail.ethz.ch...
> but what alternative do I have to the "for .. in" loop. How can I step
through
> all elements of an associative array withouth using "for .. in"

No, you can do it, just make sure that the type of the thing you get back is
the object you expect and not a function - that's real belt and braces stuff
really, if you don't prototype array, you're probably okay, and the failure
cases should be rare.

Alternatively you can do:

function Chicken() {}
a=new Chicken()
a['moo']=whatever

This will allow you to avoid the prototype risk of array (people are very
unlikely to prototype object directly), at the loss of any array like
features.

> Or should I avoid using associative arrays at all? Would be bad news. I am
quite
> used to associative arrays, coming from perl/PHP

They're okay, as long as you don't expect them to be ordered (use a real
array for that) and don't prototype anything on the array/object.

what you can do is:

a=[]
a['chicken']=whatever
a.push(a['chicken'])

for (i=0;i<a.length;i++) alert(a)

Which allows you iterate over them in order by using the array features, and
still get the associative array features.

Jim.




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Strange behaviour of Rhino with associative array object

Posted by Andreas Neumann <ne...@karto.baug.ethz.ch>.
ok - I do understand that the sorting in associative arrays is implementation
specific

but what alternative do I have to the "for .. in" loop. How can I step through
all elements of an associative array withouth using "for .. in"

Or should I avoid using associative arrays at all? Would be bad news. I am quite
used to associative arrays, coming from perl/PHP

apologize if that is a stupid question. I am not a good programmer ...

Thanks for ideas on that issue,
Andreas


Zitiere Jim Ley <ji...@jibbering.com>:

> 
> "Andreas Neumann" <ne...@karto.baug.ethz.ch> wrote in message
> news:1066454983.3f90cfc71e4ee@karmail.ethz.ch...
> 
> > Later on I use a "for ... in" loop to extract the values of the
> associative
> > array.
> 
> This isn't really safe, because things in the prototype also can get
> enumerated so if I do:
> 
> Array.prototype.chicken=function() {alert('a') }
> 
> n=[]
> n['a']=1
> 
> for (i in n) alert(i)
> 
> will alert a and chicken
> 
> > It appears that the Rhino js engine automatically sorts
> alphabetically
> > according to the index value, which means, that "Asians" come first,
> then
> "
> > Blacks", ....
> 
> Perfectly valid behaviour, there's no reliability on the ordering, it's
> implementation specific, I'd recommend rewriting to avoid needing for i
> in a
> syntax.
> 
> Jim.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 
> 



--     
The only thing that stands between a man and what he wants from
life is often merely the will to try it and the faith to believe
that it is possible ...
-- M. DeVos
    
----------------------------------------------     
Andreas Neumann - Department of Cartography     
Swiss Federal Institute of Technology (ETH)     
ETH Hoenggerberg, CH-8093  Zurich, Switzerland     
Phone: ++41-1-633 3031, Fax: ++41-1-633 1153     
e-mail: neumann@karto.baug.ethz.ch     
www: http://www.karto.ethz.ch/neumann/     
SVG.Open/Carto.net: http://www.svgopen.org/     

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Strange behaviour of Rhino with associative array object

Posted by Jim Ley <ji...@jibbering.com>.
"Andreas Neumann" <ne...@karto.baug.ethz.ch> wrote in message
news:1066454983.3f90cfc71e4ee@karmail.ethz.ch...

> Later on I use a "for ... in" loop to extract the values of the
associative
> array.

This isn't really safe, because things in the prototype also can get
enumerated so if I do:

Array.prototype.chicken=function() {alert('a') }

n=[]
n['a']=1

for (i in n) alert(i)

will alert a and chicken

> It appears that the Rhino js engine automatically sorts alphabetically
> according to the index value, which means, that "Asians" come first, then
"
> Blacks", ....

Perfectly valid behaviour, there's no reliability on the ordering, it's
implementation specific, I'd recommend rewriting to avoid needing for i in a
syntax.

Jim.




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org