On Wed, May 21, 2008 at 9:40 PM, beast <[EMAIL PROTECTED]> wrote:

> Good morning,
>
> I have 2 files which contains some IDs. Basically I want to search ID
> in the file A which is missing on the file B.
>
> This program is ugly, but its work :-)
> ------------
> use strict;
>
> my $target_file = "B.txt";
> while(<>) {
>   chomp;
>   my $res = `grep $_ $target_file`;
>   print "$_ is missing\n" if ! $res;
> }
> ------------
>
> I'm trying to found another solutions which more perlish and efficient.
> So far im relying that ID should be same digits on the both file,
> because "123" will match "1234" :-(
>
> File contents is around 20k - 30k lines, so i must consider the
> performance and memory usage also.
>
> A.txt
> -----
> 12345
> 56789
> 32134
> 62134
> 42134
> 52134
> 12234
>
> B.txt
> -----
> 12234
> 42134
> 82134
> 32134
>
>
> Thanks.
>
> --budhi
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
> in your method ,you can use egrep to search, and let grep stop after first
match
grep -e "^$_\$" -m 1 $target_file, this will prevent partial matching

I would prefer read everything in $target_file into a hash, then compare
$input_file against the hash

use strict;
use warnings;

my ($input_file,$target_file) = @ARGV;
my %hash;

open my $fh, "<", $input_file or die "Cannot open $input_file\n";
while (<$fh>) {
        chomp;
        $hash{$_}++;
}
close $fh;

open $fh, "<", $target_file or die "Cannot open $target_file\n";
while (<$fh>) {
        chomp;
        print "$_ is missing\n" unless ( defined $hash{$_});
}

Reply via email to