If I understand your stated goal, I think you just need to move the print statement for the lines from file2 so that it's outside the conditional -- that is, you are going to print every line from file2, but you will only be altering the contents of some of the lines.

Also, since you have a hash for the relevant values from file1, you don't need to loop over all the hash keys when reading each line from file2. You just need to test of the field-11 value from file2 exists as a hash key from file1.

Putting those two things together:

use strict; use warnings; my ( $file1, $file2 ) = ( 'file1_name_here', 'file2_name_here' ); # read hash keys and values from file1: open( F, '<', $file1 ) or die "$file1: $!\n"; my %mods; while (<F>) { chomp; my ( $key, $val ) = ( split /\t/ )[0,4]; $mods{$key} = $val; } close F; # read and rewrite file2, altering certain lines as needed open( IN, '<', $file2 ) or die "$file2: $!\n"; open( OUT, '>', "$file2.new" ) or die "$file2.new: $!\n"; while (<IN>) { chomp; my ( $loc, $chk ) = ( split /\t/ )[0,11]; # or should it be [0,10 +]? if ( $loc =~ /location/ ) { # modify some input lines as needed $_ .= "\t YRI_iHs\n"; } elsif ( exists( $mods{$chk} )) { $_ .= "\t$mods{$chk}\n"; } print OUT; # print every input line to output file } close IN; close OUT; # you could rename the output to replace the original... # rename "$file2.new", $file2; # (but you might want to check the output first)
If that's not what you meant, then you'll need to explain your goal more clearly, possibly with some sample data.

(updated to fix typo in last comment line)


In reply to Re^3: Add data to input files by graff
in thread Add data to input files by micky744monk

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.