473,804 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

window.alert(aS tatList[cTemp]); -> "term"
window.alert(aS tatList.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
Jul 23 '05 #1
8 1422
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).
Jul 23 '05 #2
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?
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).

Jul 23 '05 #3
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.lengt h = 0;
....
aStatList['key'] = value;
aStatList.lengt h+=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.

Jul 23 '05 #4
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.
Jul 23 '05 #5
VK wrote:
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.lengt h = 0;
...
aStatList['key'] = value;
aStatList.lengt h+=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(proper ty, value) {
if (!this.exists(p roperty)) {
collection[property] = value;
order.push(prop erty);
}
}
this.remove = function(proper ty) {
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(col lection[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(proper ty, value) {
if (value != null) {
collection[property] = value;
}
var ii = order.length;
while (ii-- > 0) {
if (order[ii] == property) {
order[ii] = null;
order.push(prop erty);
break;
}
}
}
this.exists = function(proper ty) {
return collection[property] != null;
}
}

var myCollection = new Collection();
myCollection.ad d("first", "Adam");
myCollection.ad d("second", "Eve");
myCollection.ad d("third", "Cane");
myCollection.ad d("fourth", "Abel");
myCollection.re move("second");
myCollection.ad d("fifth", "Noah");
alert(myCollect ion.toString()) ;
alert(myCollect ion.getKeys());
myCollection.up date("first");
alert(myCollect ion.toString()) ;
alert(myCollect ion.exists("thi rd"));
alert(myCollect ion.exists("som ething"));
</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.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #6
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....throu gh 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
Jul 23 '05 #7
On Sat, 4 Dec 2004 08:44:52 +1000, Tom Szabo <to*@intersoft. net.au> wrote:
OK, thanks for all the help.

I worked out what is my confusion. In one of the book I have read about
Associative Arrays.


There's no such thing in ECMAScript. That's the confusion.

I've written more about this recently. See
<URL:http://groups.google.c om/groups?threadm= opshpyymilx13kv k%40atlantis>
and the remaining posts in that thread.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
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.

"Associativ e array" was a bad term from the beginning, and it was lately
abandoned in favor of "hash".
The term "associativ e 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).le ngth; // if you really need it


Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1829
by: The Lone Wolf | last post by:
hi, i'm new to php mysql and already have a problem with a simple routine $result=mysql_db_query(jcorp,"select count(*) as bar from users"); $count = mysql_result($result,0,"bar"); //<<<<< this is line 3 in the script echo "Active Members $count";
32
2276
by: Fresh Air Rider | last post by:
Hi I understand that ASP.net 2.0 (Whidbey) is going to reduce coding by 70%. Surely this is going to de-skill or dumb down the developer's task and open up the task of web development to less qualified and trained staff. Tell me if I'm wrong.
6
3250
by: Kathy Burke | last post by:
Ugh. I'm using the following in an asp.net. I get an Syntax Error in INSERT INTO Statement on line Cmd1.ExecuteNonQuery(). I've made all my database fields text (just to eliminate that as a potential problem). I changed all my variables in the insert statement to text to test as shown below. Still getting the error! I've checked again and again my db table and field names. Could someone please tell me where to go from here??? THANKS,...
12
1620
by: Marty | last post by:
It seems all of the sudden that user controls that contain images are referencing image sources relative to the document that I drop the control on. This obviously does not work beacuase the image source is relative to the user control, not necessarily the form. Can anyone tell me why this would be the case?
4
1222
by: Johnny Emde | last post by:
Hello Newsgroup I'm about going nuts!!! Sometime when I coding a asp.net (using C#) I'll get this wierd error. Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
10
2110
by: Q | last post by:
Hi, I'm using a hashtable to store away a bunch of numbers from my array: The Hashtable should look like: Key Value 1 01234 2 01235
1
1568
by: windandwaves | last post by:
Hi I have the following file: --myfile.php <?php if($s) { echo $s; session_id($s); session_start;
5
1421
by: Shelly | last post by:
This ASPX/SqlServer stuff is driving me nuts. 1 - In a previous post I said I had four tables and all were showing up in Object Explorer (MS SQL Server Management Studio Express), but when I went to do a query using the query text editor, only two of the tables showed up. Then, mysteriously, the problem went away. Now, it is back. 2 - My insert into a table failed from the debug version in Web Developer, so I copied that SQL call and...
16
342
by: jimmy.musselwhite | last post by:
Hello all It would be great if I could make a number that can go beyond current size limitations. Is there any sort of external library that can have infinitely huge numbers? Way way way way beyond say 5x10^350 or whatever it is? I'm hitting that "inf" boundary rather fast and I can't seem to work around it. Thanks!
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.