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 );
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.