473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing two hashes

I need to check if two hashes are identical.

My thoughts are something like this:

function compareHash(has h1,hash2){

if(hash1.length != hash2.length){r eturn 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 3985
"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(has h1,hash2){

if(hash1.length != hash2.length){r eturn 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({ke y: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/rasterTriangleD OM.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(has h1,hash2){

if(hash1.length != hash2.length){r eturn 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({ke y: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/rasterTriangleD OM.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
495
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 checking to see if the result has any 1s in it (if so, the arrays are different). This seems to be faster than comparing each bit of the two original...
6
2306
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 with iterators, etc. ? (just trying to reduce code clutter)
1
5704
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 checking to see if the result has any 1s in it (if so, the arrays are different). This seems to be faster than comparing each bit of the two original...
2
3450
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 some other method to determine if a file has changed?
0
7912
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...
0
8202
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. ...
0
8338
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...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
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...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
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...
0
5390
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.