Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by ikegami (Patriarch)
on Aug 12, 2005 at 15:25 UTC ( [id://483303]=note: print w/replies, xml ) Need Help??


in reply to Substitute text string in a file with matching text from another file

Hashes are very useful for matching.

use strict; use warnings; my %lookup; { open(my $file1, '<', 'file1.txt') or die("..."); while (<$file1>) { chomp; my @fields = split(/\|/, $_); $lookup{$fields[0]}{$fields[1]} = $fields[2]; } } { open(my $file2, '<', 'file2.txt') or die("..."); while (<$file2>) { chomp; my @fields = split(/\|/, $_); if (exists $lookup{$fields[0]}{$fields[1]}) { $fields[3] = $lookup{$fields[0]}{$fields[1]}; } else { $fields[3] = 'NO MATCH'; } print(join('|', @fields), $/); } }

Tested.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found