Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by Transient (Hermit)
on Aug 12, 2005 at 15:47 UTC ( [id://483316]=note: print w/replies, xml ) Need Help??


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

#!/usr/bin/perl use strict; use warnings; # substituted for the FILE my @file2 = qw(789|efg|2222222 123|abc|9999999 786|uvw|1234567 123|xxx +|0000000 234|cde|0000000 567|xyz|1111111); # process file 1 my %file_1_hash = (); while ( <DATA> ) { chomp; my @array = split( /\|/ ); $file_1_hash{join( '|', @array[0..$#array-1] )} = $array[$#array]; } # when file2 is a FILE, just do a while loop here foreach (@file2) { my @file_2_array = split( /\|/ ); my $file_2_key = join( '|', @file_2_array[0..$#file_2_array-1] ); if ( !exists $file_1_hash{$file_2_key} ) { $file_1_hash{$file_2_key} = "NO MATCH"; } print join( '|', $file_2_key, $file_2_array[$#file_2_array], $file_1 +_hash{$file_2_key} ), "\n"; } __DATA__ 123|abc|777 234|cde|456 567|xyz|999

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-29 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found