develooper Front page | perl.beginners | Postings from February 2002

Re: ignoring some fields in a comparision of hashes

Thread Previous | Thread Next
Jeff 'japhy' Pinyan
February 7, 2002 10:21
Re: ignoring some fields in a comparision of hashes
Message ID:
On Feb 7, Chas Owens said:

>I have two hashes (%a and %b) that contain data for one person from two
>different systems and I want to compare these hashes to see if the
>systems are out of sync.  The catch is I know that some of the fields
>will always be different and I want to ignore those fields.  Below is my
>solution, does anyone have a better way of doing this? BTW: there are a
>lot of fields currently with more being added as time goes on and the
>number of fields I want to ignore will stay pretty much the same).

You're already using hashes!  The better solution to your problem is to
make a hash of keys to ignore.

>my @ignore = ("key1", "key2");

  my %ignore;
  @ignore{ "key1", "key2" } = ();

>KEYS: foreach my $key (keys %a) {

>  foreach my $ignore (@ignore) {
>    next KEYS if $key eq $ignore;
>  }

    next KEYS if exists $ignore{$key};

>	if ($a{$key} ne $b{$key}) {
>		print "$key is different ($a{$key}, $b{$key})\n";
>	}

Jeff "japhy" Pinyan
RPI Acacia brother #734
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About