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 | |
by gghelpneeded (Novice) on Jul 18, 2015 at 00:06 UTC | |
by tangent (Parson) on Jul 18, 2015 at 00:46 UTC | |
|
Re: Table Matching
by tangent (Parson) on Jul 17, 2015 at 23:14 UTC | |
by gghelpneeded (Novice) on Jul 18, 2015 at 00:02 UTC |