JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 31st, 2002, 01:17 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
hash tables in javascript?

Can you do hash tables in javascript?
__________________
Matthew Doucette / Xona.com

Reply With Quote
  #2  
Old August 31st, 2002, 02:29 PM
Tuxie Tuxie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Inside the GNU/Hurd kernel
Posts: 492 Tuxie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m
Reputation Power: 12
Yes,read the Objects section at http://www.crockford.com/javascript/survey.html
Comments on this post
Matthew Doucette agrees!

Reply With Quote
  #3  
Old August 31st, 2002, 02:39 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
Thanks. Now, can you show me how to define multiple hashes all at once instead of one at a time?

Reply With Quote
  #4  
Old August 31st, 2002, 03:33 PM
Tuxie Tuxie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Inside the GNU/Hurd kernel
Posts: 492 Tuxie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m
Reputation Power: 12
Yeah,heres an example:
Code:
var myHashtable = {name: "Someone",age: "2"};


So a list of <key>:<value> delimited by commas,in a block.

Reply With Quote
  #5  
Old September 1st, 2002, 10:09 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
I should have guessed that but I figured I would ask anyway. It's hard to tell when you have proper formatting, even when it works, with client-side languages.

Thanks!

Reply With Quote
  #6  
Old September 1st, 2002, 10:29 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
One more thing... is there any way to calculate the length of the hash table instead of going through each entry in a loop and incrementing a counter? Also, once you figure out the length, is there anyway to grab a random hash entry? For example, if the hash is 100 entries in lenght and I want to grab #25, can I do this?

Reply With Quote
  #7  
Old September 1st, 2002, 09:31 PM
adios adios is offline
Senior Citizen
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2001
Location: leftcoast
Posts: 2,019 adios User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
Using a for/in loop on a hash returns the entries in the order in which they were defined; so...

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var hashObj = Object;
hashObj['1'] = 'hash1';
hashObj['2'] = 'hash2';
hashObj['3'] = 'hash3';
hashObj['4'] = 'hash4';
hashObj['5'] = 'hash5';

function gethashObj(which) {
var count = 1;
for (key in hashObj) if (count++ == which) return hashObj[key];
}

function go() {
x=Math.ceil(Math.random()*5);
alert('random number = ' + x);
alert('hashObj = ' + gethashObj(x));
}

</script>
</head>
<body>
<a href="#" onclick="go()">go</a>
</body>
</html>

As for the other...JS hashes are unordered, unlike arrays, so additional coding is needed (I believe) to get 'length'.

Reply With Quote
  #8  
Old September 1st, 2002, 09:50 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
Ok. Without being able to find the length 'easily' I am just going to use 2 arrays instead of 1 hash table. I think that is the easiest and most compact method. Thanks for the help!

Reply With Quote
  #9  
Old September 2nd, 2002, 07:03 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 13
Yup, you're going to need to use two arrays.

If you want to go all out, best create a class with the arrays as memebers, that way you can add functions and handlers for your hash tables and create an all-inclusive map class that will keep an internal count of everything. That's on my list of things to do a project of mine.

Reply With Quote
  #10  
Old September 3rd, 2002, 05:04 PM
adios adios is offline
Senior Citizen
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2001
Location: leftcoast
Posts: 2,019 adios User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

Object.prototype.getLength = function() {
len = 0;
for (prop in this) if (this[prop].constructor != Function) ++len;
return len;
}

Object.prototype.output = function (isHTML) {
var hStr = '';
for (prop in window) if (window[prop] == this) hStr += 'Obj: ' + prop + '\n';
for (prop in this) if (this[prop].constructor != Function)
hStr += prop + ' : ' + this[prop] + ((isHTML) ? '<br>' : '\n');
return hStr;
}
///////////////////////
var myHash = {
prop1: 'thingy' ,
prop2: 'anotherthingy' ,
prop3: 'yetanotherthingy' ,
prop4: 'non-thingy'
}
///////////////////////

</script>
</head>
<body text="darkred"><pre>
var myHash = {
prop1: 'thingy' ,
prop2: 'anotherthingy' ,
prop3: 'yetanotherthingy' ,
prop4: 'non-thingy'
}
</pre><br><br>

<a href="#"
onclick="if(myHash)alert(myHash.getLength())">myHash.getLength()</a><br><br>
<a href="#"
onclick="if(myHash)alert(myHash.output())">myHash.output()</a><br><br>
<a href="#"
onclick="if(myHash)this.innerHTML=myHash.output(true)">myHash.output(true)</a>
</body>
</html>

Last edited by adios : September 3rd, 2002 at 05:32 PM.

Reply With Quote
  #11  
Old September 3rd, 2002, 06:19 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 13
Damn, adios, your example finally made me notice something. I just caught on that if you assign an array element like myHash['foo']='bar', you can actually get back what the key is. I thought the 'bar' string was hashed, translated to an index, and lost forever after the assignment. Suddenly, an Array object is almost a true hash table. All that I need to do is generate a simple function for searching, which can be done by extending the Array object with a prototype.

It couldn't get much easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > hash tables in javascript?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap