473,408 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 3937
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
6
by: mahurshi | last post by:
i have two hashes (maps) map <int, int> FAULTFREENETS; map <int, int> FAULTNETS; is there a way i can check if FAULTFREENETS = FAULTNETS with just one statement instead of writing a for loop...
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
2
by: kj7ny | last post by:
I am attempting to incrementally back up files using python. How can I compare a file on a hard drive to a file in a python created zip file to tell if the file has changed? Or should I be using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.