in reply to Add data to input files

Now I have a tab delimited text file2 with 15 fields and I want to add the info of field 5 from file1, if the field 11 (of file 2) is equal to the field 1 of file 1.

this is more tricky, temptatively and untested, open file 2 first as usual...

for my $key ( keys %hash ) { while ($file2){ if ( / \a .*?\t # field 1 of file 2 .*?\t # field 2 of file 2 .*?\t # field 3 of file 2 .*?\t # field 4 of file 2 .*?\t # field 5 of file 2 .*?\t # field 6 of file 2 .*?\t # field 7 of file 2 .*?\t # field 8 of file 2 .*?\t # field 9 of file 2 .*?\t # field 10 of file 2 $key \t .*?\t # field 12 of file 2 .*?\t # field 13 of file 2 .*?\t # field 14 of file 2 .*\z # field 15 of file 2 /x) { do something...} else {next} }

Replies are listed 'Best First'.
Re^2: Add data to input files
by micky744monk (Novice) on Sep 18, 2011 at 20:32 UTC
    I did not understand your solution Actually the code till here is
    while (<MYINFILE>) { chomp ($_); @line = split (/\t/, $_); $rsfound = $line[11] ; my$loc = $line[0] ; if ($loc =~ m/location/i) {print OUTFILE "$_\t YRI_iHs\n" ; } elsif ($rsfound =~ m/^rs[0-9]/i) {foreach my$key (keys(%out)) { if ( $rsfound eq $key ) {print OUTFILE "$_\t $out{$key}\n";} }
    This script show me only the line of file 2 where i find the match with the key of the hash %out. I actually need to print all the lines, even the ones without the match. Sorry from file 1 I go the hash %out with field1 as keys and field5 as values
      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)

        That was perfect. My problem was to iterate along all the keys of my hash. I should revise the function exists!!! Cheers for replies!