Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Process multiple files in a directory

by aaron_baugher (Curate)
on Jun 27, 2015 at 17:51 UTC ( #1132285=note: print w/replies, xml ) Need Help??


in reply to Process multiple files in a directory

Others have already pointed out your mistakes and provided basic working versions, so here's one using a couple of newer features (for/when and say) to get rid of some of the cruft, which you may be interested in as you learn:

#!/usr/bin/env perl use 5.010; use strict; use warnings; for my $file (<*>){ # get all filenames in current director +y say "===== $file ====="; open my $fd, '<', $file or die $!; my $count = 0; # counter for /failed/ matches while(my $line = <$fd>){ for($line){ print when /Total/; # print matching lines $count++ when /failed/; # count matching lines } } say "Total Volume Failed to Resolve: $count"; close $fd; }

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Replies are listed 'Best First'.
Re^2: Process multiple files in a directory
by kcott (Bishop) on Jun 28, 2015 at 01:21 UTC

    G'day Aaron,

    I wouldn't recommend showing someone, who's "new to perl", experimental features (without clearly pointing out that they are experimental and why you are using them). I'm not sure what born_Today will make of the follow two lines on stderr:

    when is experimental at <script_name> line 10. when is experimental at <script_name> line 11.

    This code seems overly complicated:

    while(my $line = <$fd>){ for($line){ print when /Total/; # print matching lines $count++ when /failed/; # count matching lines } }

    This may have been better:

    while (<$fd>) { print if /Total/; # print matching lines $count++ if /failed/; # count matching lines }

    -- Ken

      You're right, I got it stuck in my head that I was going to demonstrate for/when, but didn't actually need it at all. I knew something wasn't right there but couldn't put my finger on it. Thanks for pointing it out, and you're right that I should have said it's experimental and limited to newer versions.

      Aaron B.
      Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2020-05-26 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (150 votes). Check out past polls.

    Notices?