in reply to Re^5: match pattern from two different file
in thread match pattern from two different file
poj#!/usr/local/bin/perl use strict; use warnings; my $pattern = qr'^LOC_Os0[1-7]g[0-9]*.[0-9]\s'; # read files my $hash1 = parse_file('file1.txt'); my $hash2 = parse_file('file2.txt'); # report my $count=0; for my $loc (sort keys %$hash1){ if ( exists $hash2->{$loc} ){ print "$loc\n"; ++$count; } } print "$count unique patterns in both files\n"; # parse file sub parse_file{ my $filename = shift; my %hash=(); my $count=0; open FH,'<',$filename or die "Can't Open $filename\n"; while ( my $line = <FH> ){ if ( $line =~ /($pattern)/ ){ $hash{$1} = 1; } ++$count; } close FH; print "$count lines read from $filename\n"; return \%hash; }
|
|---|