in reply to Adding some columns into a file from two files using perl

You could first save the data from the second file in a hash with the first column as a key, and then retrieve that data from the hash as you're going over the lines from the first file. The following code assumes that the first columns in the two files are identical (string-wise). Also this will read the entire second file into memory, so it might not be too great if your second file becomes huge.

# ... snip opening the files ... my %file2data; while(<$input_file2>) { next if /(^\s*$)|(^#)|(^@)/; my @columns2 = split; $file2data{$columns2[0]} = $columns2[1]; } while(<$input_file1>) { next if /(^\s*$)|(^#)|(^@)/; my @columns1 = split; print $out_file join("\t", $columns1[0],$columns1[1], $file2data{$columns1[0]}), "\n"; }

This will produce your expected output.

Note that if you're going to be manipulating a lot of files like this, then investing the time into learning Text::CSV will probably be worth it. Also, if your dataset is large, this is the kind of operation that a database would handle well.

Replies are listed 'Best First'.
Re^2: Adding some columns into a file from two files using perl
by perlselami (Initiate) on Jan 19, 2015 at 13:52 UTC
    @Anonymous Monk, thanks for your reply. But I get the following error. How can i fixed it?
    Use of uninitialized value in join or string at ./rmsd2.pl line 30, <$ +input_file1> line 14.

      Try adding use diagnostics; at the top of your program to get explanations of the messages. In this case it's just a warning, not an error, and since you seem to be running your code with different input files than the ones in the OP (they don't have 14 lines), and I don't know what line 30 of your program does, I can only take a wild guess: Maybe $file2data{$columns1[0]} is empty, because the first columns of the files don't match up exactly. As stated, the example code doesn't really handle that case, but you could fairly easily modify it so it does, depending on what your input actually looks like. Without seeing a more representative sample of your input and your current program it's hard to say what the best solution is. See also the Basic debugging checklist.

        You are wonderful. Thanks. I used my original input files and it works correctly. But i will try to learn Text::CSV. I hope i will find some useful examples about Text::CSV on internet.
        my final code: #!/usr/bin/perl use strict; use warnings; use diagnostics; # Open file1 to read open my $input_file1, '<', "rmsd1.xvg" or die qq{Failed to open "rmsd1 +.xvg" for writing: $!}; # Open file2 to read open my $input_file2, '<', "rmsd2.xvg" or die qq{Failed to open "rmsd2 +.xvg" for writing: $!}; # Open new file to write open my $out_file, '>', "rmsd.xvg" or die qq{Failed to open "rmsd.xvg" + for writing: $!}; # Copy data from one file to another. my %file2data; while(<$input_file2>) { next if /(^\s*$)|(^#)|(^@)/; my @columns2 = split; $file2data{$columns2[0]} = $columns2[1]; } while(<$input_file1>) { next if /(^\s*$)|(^#)|(^@)/; my @columns1 = split; print $out_file join("\t", $columns1[0],$columns1[1], $file2data{$col +umns1[0]}), "\n"; } close($input_file2); close($input_file1); close($out_file);