Am I going nuts???? Array problem!?

T

Tom Szabo

cTemp = oNode.cName;
aStatList[cTemp] = "term";

window.alert(aStatList[cTemp]); -> "term"
window.alert(aStatList.length); -> "0"

the above code is going to display "term" and "0"

That is it somehow adds/sets the value of the intended associative array
"aStatList[cTemp]" to "term" but beleives the length is still zero...

What am I overlooking....

If I change the index from cTemp to an interger it will work. In the above
case cTemp is something like "d1"

TIA,

Tom
 
V

VK

Because you are not using the Array object (you cannot use it for this
purpose, chicken is not a bird, and array is not a hash)
You are hashing the generic Object, which supports this trick.

So:
aStatList isInstanceOf Object (not array)
Object.length gives you 0 (actually, should be null or undef, but maybe I
just don't remember the generic Object's structure).
 
T

Tom Szabo

Because you are not using the Array object (you cannot use it for this
purpose, chicken is not a bird, and array is not a hash)

So how do I do it? Where is the difference?

I start with :
var aStatList = new Array();

What should I be doing?
 
V

VK

1) You may construct your own Hash object using Object as prototype (the
most academically proper way, but may be boring and time consuming).

2) You may try Enumerator object, but I believe it's full on bugs and IE
only.

3) You may simply imitate enumeration method, something like:

var aStatList = new Object();
aStatList.length = 0;
....
aStatList['key'] = value;
aStatList.length+=1;
....

P.S. And don't expect to have your key/value pairs in the order you put them
there. Hash rearrange pairs in its own internal order, which is for human
perception is a total disorder.
 
V

VK

var aStatList = new Array();

It doesn't matter. You could use new String(), Boolean(), Date() etc.

By making assignment like obj['key']=value, you are using generic Object's
properties, which is not a problem, because any object extends class Object.
 
G

Grant Wagner

VK said:
1) You may construct your own Hash object using Object as prototype (the
most academically proper way, but may be boring and time consuming).

2) You may try Enumerator object, but I believe it's full on bugs and IE
only.

3) You may simply imitate enumeration method, something like:

var aStatList = new Object();
aStatList.length = 0;
...
aStatList['key'] = value;
aStatList.length+=1;

The problem with your approach is that:

for (var key in aStatList) {
// key == 'length' will be true at some point through this loop
}

Actually, -key- might also contain other enumerable properties of the Object
object as well.
P.S. And don't expect to have your key/value pairs in the order you put them
there. Hash rearrange pairs in its own internal order, which is for human
perception is a total disorder.

One way to avoid this is to store an array of keys and the actual value as a
property of an object. That way you avoid enumerable objects, and you can
control the order the order objects appear in:

<script type="text/javascript">
function Collection() {
var collection = {};
var order = [];

this.add = function(property, value) {
if (!this.exists(property)) {
collection[property] = value;
order.push(property);
}
}
this.remove = function(property) {
collection[property] = null;
var ii = order.length;
while (ii-- > 0) {
if (order[ii] == property) {
order[ii] = null;
break;
}
}
}
this.toString = function() {
var output = [];
for (var ii = 0; ii < order.length; ++ii) {
if (order[ii] != null) {
output.push(collection[order[ii]]);
}
}
return output;
}
this.getKeys = function() {
var keys = [];
for (var ii = 0; ii < order.length; ++ii) {
if (order[ii] != null) {
keys.push(order[ii]);
}
}
return keys;
}
this.update = function(property, value) {
if (value != null) {
collection[property] = value;
}
var ii = order.length;
while (ii-- > 0) {
if (order[ii] == property) {
order[ii] = null;
order.push(property);
break;
}
}
}
this.exists = function(property) {
return collection[property] != null;
}
}

var myCollection = new Collection();
myCollection.add("first", "Adam");
myCollection.add("second", "Eve");
myCollection.add("third", "Cane");
myCollection.add("fourth", "Abel");
myCollection.remove("second");
myCollection.add("fifth", "Noah");
alert(myCollection.toString());
alert(myCollection.getKeys());
myCollection.update("first");
alert(myCollection.toString());
alert(myCollection.exists("third"));
alert(myCollection.exists("something"));
</script>

Note that this is not a fully-featured (or even that well thought-out)
Collection object. It's just an example of how to store arbitrary key/value
pairs in an ordered list.

An improvement to the design would be to append a randomly generated
prefix/suffix to each -key-, to be sure to avoid conflicts with existing
property/method names.
 
T

Tom Szabo

OK, thanks for all the help.

I worked out what is my confusion. In one of the book I have read about
Associative Arrays. It looked very much the same as in PHP. Except the guy
did not say that one can't check the length....through aArray.length
property....

Anyway, it is hard to beleive, but looks like through: you declare something
like an array, you call it as an array, but it is not an array...

Thank for all your help anyway,

Regards,

Tom
 
V

VK

Anyway, it is hard to beleive, but looks like through: you declare something
like an array, you call it as an array, but it is not an array...

The magic of words... Sometimes an inadequate term may result in a wrong
perception of the object's nature == wrong coding approach.

"Associative array" was a bad term from the beginning, and it was lately
abandoned in favor of "hash".
The term "associative array" makes you think, that it's still some Array but
with a twist.
In the reality Array and Hash have no more common than an elephant and a
lion (they are both mammals, and that's it).

The top of the Hash evolution (properties/methods set) has been reached in
Perl, and even there %hash did not have a 'length' property, because it's
useless and irrelevant for the hash nature.
You are working with hash keys set, not with indexes.

var myHash = new Object();
....
function keys(oHash) {
var aKeys = new Array();
for (prop in oHash) {
aKeys.push(prop);
}
return aKeys;
}
....
var hashKeys = keys(myHash);
var hashLength = keys(myHash).length; // if you really need it
 

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
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top