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


In reply to Re: Substitute text string in a file with matching text from another file by jch341277
in thread Substitute text string in a file with matching text from another file by tariqahsan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.