ignoring some fields in a comparision of hashes
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). example my @ignore = (key1, key2); KEYS: foreach my $key (keys %a) { foreach my $ignore (@ignore) { next KEYS if $key eq $ignore; } if ($a{$key} ne $b{$key}) { print $key is different ($a{$key}, $b{$key})\n; } } /example -- Today is Pungenday the 38th day of Chaos in the YOLD 3168 All Hail Discordia! Missle Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ignoring some fields in a comparision of hashes
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. example 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; } } /example -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ignoring some fields in a comparision of hashes
On Thu, 2002-02-07 at 13:21, Jeff 'japhy' Pinyan wrote: 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. example 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; } } /example -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course. Doh! Now I feel like an idiot. I knew what I was doing was too complicated. -- Today is Pungenday the 38th day of Chaos in the YOLD 3168 Grudnuk demand sustenance! Missle Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: ignoring some fields in a comparision of hashes
%ignore = map { $_ = 1 } @ignore; @diff = map { $a{$_} ne $b{$_} } grep { !$ignore{$_} } keys %a; @diff will end up with a list of key/value pairs of %a that are different from %b, excluding those of %ignore. note: this is untested. Chris On 7 Feb 2002, Chas Owens wrote: 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). example my @ignore = (key1, key2); KEYS: foreach my $key (keys %a) { foreach my $ignore (@ignore) { next KEYS if $key eq $ignore; } if ($a{$key} ne $b{$key}) { print $key is different ($a{$key}, $b{$key})\n; } } /example -- Today is Pungenday the 38th day of Chaos in the YOLD 3168 All Hail Discordia! Missle Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]