May be you can try to read data in hash first as it is an index data, then match key to it and add the value. I am assuming your master data in the format that you have provided.
#!/usr/bin/perl -w use strict; my $file1 = "file1.txt"; my $file2 = "file2.txt"; my %hash = (); my %ahash = (); open F1, $file1 or die "Can't open $file1 $!"; open F2, $file2 or die "Can't open $file2 $!"; while(<F1>){ my $line = $_; chomp($line); $hash{$line} = $line; } while(<F2>){ my $line = $_; chomp($line); (my $w1, my $w2) = split(/ +/, $line); $ahash{$w1} = $w2; } # Matching of two Hash for ( keys %hash ) { unless ( exists $ahash{$_} ) { print "$_: not found in second hash\n"; next; } if ( $hash{$_} eq $ahash{$_} ) { print "$_: values are equal\n"; # Do something with the value } else { print "$_: values are not equal\n"; } }
Might be it will give you an idea to compare files data using hash.
In reply to Re: Better way to search an Array?
by akuk
in thread Better way to search an Array?
by doubleqq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |