in reply to Re: iterating through a hash?
in thread iterating through a hash?
(updated the code so that the names are always printed with the values from file1, as per the OP spec.)use strict; my %hash; open( F, "<", "file1" ) or die "file1: $!"; while (<F>) { my ( $name, $val ) = split; $hash{$name} = $val; } open( F, "<", "file2" ) or die "file2: $!"; while (<F>) { my ( $name, $val ) = split; if ( exists( $hash{$name} ) { my $status = ( $hash{$name} eq $val ) ? 'OK':'WRONG'; print "$name\t$status\t$hash{$name}\n"; delete $hash{$name}; # don't need this anymore } } # any keys left in hash are cases where a file1 name was not found in +file2 # so print those now: if ( keys %hash ) { print "\nNames not found in file2:\n"; print "$_\t$hash{$_}\n" for ( sort keys %hash ); }
Nothing happens for names in file2 that don't exist in file1, but the OP didn't say that anything needed to be done for those. There was also nothing said about the same name occurring more than once in either file, but maybe the AM won't need to worry about that...
|
|---|