Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Filtering Output from two files

by LanX (Saint)
on Feb 05, 2018 at 12:10 UTC ( [id://1208477]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Filtering Output from two files
in thread Filtering Output from two files

You have at least one bug of forgetting sigil $ in line once, but yes this was the basic idea.

And dumping inside the loop is costly.

Furthrmore you might want to use <code> tags next time. :)

Minor nitpick: when setting value 1, you don't need exists anymore. :)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: Filtering Output from two files
by vighneshmufc (Acolyte) on Feb 05, 2018 at 15:00 UTC
    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.
      > It is me who posted anonymously 

      I'm glad you are making progress. :)

      > I didn't get where I missed "sigil $ in line once". 

      $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.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Wikisyntax for the Monastery

Re^4: Filtering Output from two files
by Anonymous Monk on Feb 06, 2018 at 04:17 UTC
    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?

      Anonymous Monk:

      What am i doing wrong?
      1. When you're calling Dumper, you're giving it a hash as an argument. The hash expands to a list of key/value pairs, and each of those is being printed as a separate value. You want to pass it a reference to the hash, like:
        print Dumper(\%hash);
      2. You should put your print statement after the end of your loop. As it is now, you're printing the hash after adding each line to it, so you're getting far more output than you really want. (Unless, of course, you're really wanting to see the hash after each line is added.)
      3. If you're on a windows box, then chomp may not be removing all the whitespace at the end of the line. I generally avoid chomp and use something like:
        $line =~ s/\s+$//; # trim whitespace

      If you're still not getting output, you may want to add:

      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.

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1208477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found