470,614 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

comparing two hashes

I need to check if two hashes are identical.

My thoughts are something like this:

function compareHash(hash1,hash2){

if(hash1.length != hash2.length){return false}

for(var key in hash1){
if(hash1[key] != hash2[key]) return false;
}
return true;
}

Have I missed something, or is there a more direct approach?

Jeff
Jul 20 '05 #1
2 3238
"Jeff Thies" <no****@nospam.net> writes:
I need to check if two hashes are identical.
What is a hash?

My guess is that it is an array that you use to store values in. You
use it as a hash *table* (and you should just use a plain object, not
an array).
My thoughts are something like this:

function compareHash(hash1,hash2){

if(hash1.length != hash2.length){return false}
Not really interesting if you have an array. Not interesting at all if
you don't.
for(var key in hash1){
if(hash1[key] != hash2[key]) return false;
You should compare with !== instead. Otherwise this comparison of two
clearly different objects gives true:
compareHash({key:0},{key:false})
Have I missed something, or is there a more direct approach?


Use objects for hash tables, drop the length comparison, and use !==.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
> > I need to check if two hashes are identical.

What is a hash?
I don't know what it's called in javascript

var hash={'item1': 'value1' , 'item2': 'value2'}

or

var HASH =new Array();

HASH['some_key']='some_value';
My guess is that it is an array that you use to store values in. You
use it as a hash *table* (and you should just use a plain object, not
an array).
Good advice. What I'm trying to do is save this to a temp cookie.

something like:

var hash={'item1' :value1','item2': 'value2'}

document.cookie="my_cookie=[" + escape(hash) + "],["+ escape(hash2) + ..;

I won't always no what the "keys" are so it seemed a good idea to send
them along.

My thoughts are something like this:

function compareHash(hash1,hash2){

if(hash1.length != hash2.length){return false}
Not really interesting if you have an array. Not interesting at all if
you don't.
for(var key in hash1){
if(hash1[key] != hash2[key]) return false;


You should compare with !== instead.


an object comparison? Didn't know that! Seems like hash1[key] is a variable
that could be compared that way.
Otherwise this comparison of two
clearly different objects gives true:
compareHash({key:0},{key:false})
Duh! slaps head.
Have I missed something, or is there a more direct approach?


Use objects for hash tables, drop the length comparison, and use !==.


Will do.

Thanks,
Jeff
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.