comparing two hashes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Thies

    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


  • Lasse Reichstein Nielsen

    #2
    Re: comparing two hashes

    "Jeff Thies" <nospam@nospam. net> writes:
    [color=blue]
    > I need to check if two hashes are identical.[/color]

    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).
    [color=blue]
    > My thoughts are something like this:
    >
    > function compareHash(has h1,hash2){
    >
    > if(hash1.length != hash2.length){r eturn false}[/color]

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

    You should compare with !== instead. Otherwise this comparison of two
    clearly different objects gives true:
    compareHash({ke y:0},{key:false })
    [color=blue]
    > Have I missed something, or is there a more direct approach?[/color]

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

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Jeff Thies

      #3
      Re: comparing two hashes

      > > I need to check if two hashes are identical.[color=blue]
      >
      > What is a hash?[/color]

      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';


      [color=blue]
      > 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).[/color]

      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.

      [color=blue]
      >[color=green]
      > > My thoughts are something like this:
      > >
      > > function compareHash(has h1,hash2){
      > >
      > > if(hash1.length != hash2.length){r eturn false}[/color]
      >
      > Not really interesting if you have an array. Not interesting at all if
      > you don't.
      >[color=green]
      > > for(var key in hash1){
      > > if(hash1[key] != hash2[key]) return false;[/color]
      >
      > You should compare with !== instead.[/color]

      an object comparison? Didn't know that! Seems like hash1[key] is a variable
      that could be compared that way.
      [color=blue]
      > Otherwise this comparison of two
      > clearly different objects gives true:
      > compareHash({ke y:0},{key:false })[/color]

      Duh! slaps head.
      [color=blue][color=green]
      > > Have I missed something, or is there a more direct approach?[/color]
      >
      > Use objects for hash tables, drop the length comparison, and use !==.[/color]

      Will do.

      Thanks,
      Jeff[color=blue]
      >
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > DHTML Death Colors:[/color]
      <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
      > 'Faith without judgement merely degrades the spirit divine.'[/color]


      Comment

      Working...