Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Substitute text string in a file with matching text from another file

by jch341277 (Sexton)
on Aug 12, 2005 at 15:46 UTC ( [id://483315]=note: print w/replies, xml ) Need Help??


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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://483315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-19 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found