It is me who posted anonymously since we aren't allowed to log in at the university.
I didn't get where I missed "sigil $ in line once".
Furthermore, i am getting errors such as Use of uninitialized value in line 34 at variables $row and $fields at the penultimate line.
So when i place the print $row if exists $hash{$fields[0]}; after the loop it gives an error "Perl requires explicit package name"
i am really a novice at this if you could guide me in this I would learn more.
| [reply] [d/l] |
$hash{line}=1;
# should be
$hash{$line}=1;
> Use of uninitialized value in line 34
You probably parse empty lines resulting in undefined variables.
Skip them with next , compare Re: Filtering Output from two files
> after the loop
The print belongs inside the loop.
please use code tags and indentation in your next post.
| [reply] [d/l] |
use strict; <br>
use warnings; <br>
use Data::Dumper; <br>
my $file1 = 'file1';<br>
my $file2 = 'file2'; <br>
#reading file1 into a hash<br>
my %hash;<br>
open (my $fh,'<',$file2) or die $!;<br>
while(my $line=<$fh>)<br>
{<br>
chomp $line;<br>
$hash{$line}=1;<br>
print Dumper %hash;<br>
}<br>
close $fh;<br>
#reading file2 line by line
<br>
open (my ($fh2),'<',$file1) or die $!;
<br>
while (my ($row) = <$fh2>) {<br>
chomp $row;<br>
# next if $row =~ /^\s*$/;<br>
my (@fields) = split(/\|/, $row);<br>
print $row if exists $hash{$fields[0]};<br>
}<br>
close $fh2;<br>
I aint getting any O/P plus when i print Dumper %hash i get loads of $VAR
What am i doing wrong?
| [reply] [d/l] |
print "<$fields[0]>\n";
in the loop where you're searching for, so you can see what key is being looked for, and compare that against the keys displayed by Dumper previously.
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
damn just realised i messed up the print statement inside the loop as u said i had to put it out of the loop.
sorry i am very new to programming hence the mistakes
| [reply] |