in reply to Substitute text string in a file with matching text from another file
Hashes are good for this kind of thing:
use strict; use warnings; use Carp; my $hr_f1 = getlines($ARGV[0]); open(IN,"<$ARGV[1]") or croak "unable to open $ARGV[1]: $!"; while(my $l = <IN>) { my ($k,$v) = getkv($l); if (defined $hr_f1->{$k}) { print "$k|$v|$hr_f1->{$k}\n"; } else { print "$k|$v|NO MATCH\n"; } } sub getlines { my $fn = shift; my %h = (); open(IN,"<$fn") or croak "unable to open $fn: $!"; while(my $l = <IN>) { my ($k,$v) = getkv($l); $h{$k} = $v; } close(IN); return \%h; } sub getkv { my $l = shift; chomp $l; my @l = split /\|/, $l; return "$l[0]|$l[1]","$l[2]"; }
and tested:
$ ./join.pl f1 f2 789|efg|2222222|NO MATCH 123|abc|9999999|777 786|uvw|1234567|NO MATCH 123|xxx|0000000|NO MATCH 234|cde|0000000|456 567|xyz|1111111|999
Update: This is the same as ikegami's solution...guess i wasn't quick enough...
|
|---|