gghelpneeded has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks. I have ran into a problem with the task at hand. The script below is designed to read a file and take what is in the first column and if it matches the fifth column of the second to print out its corresponding row of data.

File1 Sample Data Input

name 17 name2 14 name3 14

File2 Sampe Data Input

1 2 3 4 name2 6 7 8 9 10 11 12 13 14 15 1 77 5 4 name2 6 9 8 9 10 11 12 13 14 15 1 2 3 4 name5 6 7 8 9 10 11 12 13 14 15

Desired Output

1 2 3 4 name2 6 7 8 9 10 11 12 13 14 15 1 77 5 4 name2 6 9 8 9 10 11 12 13 14 15

code

open(FILE1, $ARGV[0]) or die "Cannot open the file: $!"; my $name while(my $line = <FILE1>) { my @data = split(" ", $line); $name = "$data[0]"; } open(FILE2, $ARGV[0]) or die "Cannot open the file: $!"; while(my $line = <FILE2>) { my @data2 = split(" ", $line); my $element = "$data2[4]"; if ($element = $name) { print "$data2[0]\t$data2[1]\t$data2[2]\t$data2[3]\t$data2[4]\t$dat +a2[5]\t$data2[6]\t$data2[7]\t$data2[8]\t$data2[9]\t$data2[10]\t$data2 +[11]\t$data2[13]\t$data2[14]\n"; } } close FILE1; close FILE2; exit;

the first part of the script prints the "$data[0]" works fine. I even tested it by itself by asking it to print "$name". The second part where I assigned "$data[0]" to the variable $name is what is giving me issues. I don't think it is recognizing it and not fulfilling the if then statement before the print. Am I approaching this incorrectly through

pseudo code

if column 5 FILE2 = column 1 of FILE1

print row

Replies are listed 'Best First'.
Re: Table Matching
by AnomalousMonk (Archbishop) on Jul 17, 2015 at 23:35 UTC

    There are many features of your OPed code that suggest it is not your real code, e.g., it cannot compile. Please post short, self-contained, compilable code examples. Please see the Short, Self Contained, Correct (Compilable), Example.

    Update: Also, please use  <c> ... </c> or  <code> ... </code> tags around code, data, and input/output. Please see Markup in the Monastery and Writeup Formatting Tips. If the two OPed example data files had been so wrapped, there would have been a nice  [download] link to click for anyone who wanted to get your example data. As it is, a process of editing the posted data is needed, which may discourage many from bothering. Please help us to help you.


    Give a man a fish:  <%-(-(-(-<

      this was my code, i thought inserting it with my error and pointing out what i thought it was, i could be pointed in the right direction. the first part of the code works by itself. the second part before the print was part giving me the issue. do you recommend not uploading it entirely if that part does not work?

      sorry wasnt aware of the sample data also being part of the code tags

        It's not that your code doesn't work, in the sense of it not doing what you want, it's that it doesn't run as it is (copy and paste it yourself and try). Follow the links AnomalousMonk has given you and you will see what is meant.
Re: Table Matching
by tangent (Parson) on Jul 17, 2015 at 23:14 UTC
    In your example $name will contain the value of the last line of file1 only, as you overwrite it on each pass through the loop. I suspect you want all the names in file1, so you can store them in a hash instead. Also, if ($element = $name) will always be true, what you need is if ($element eq $name). If I am right about you wanting to check all file1 names then you could do:
    use strict; use warnings; use Data::Dumper; my $file1 = $ARGV[0]; my $file2 = $ARGV[1]; my %names; open( my $fh1, '<', $file1 ) or die "Cannot open $file1: $!"; while (my $line = <$fh1>) { my @data = split(" ", $line); $names{ $data[0] }++; } close( $fh1 ); # have a look to see what is in %names print Dumper( \%names ); open( my $fh2, '<', $file2 ) or die "Cannot open $file2: $!"; while (my $line = <$fh2>) { my @data2 = split(" ", $line); my $element = $data2[4]; if ( $names{ $element } ) { print join("\t", @data2 ), "\n"; } } close( $fh2 );

      when dealing with two files (as a general concept) should one be thrown into a hash, then that hash be used to analyze it? Because initially I thought a simple comparison of each column would suffice. But as you pointed out my loop only left me with the value of the last line.