in reply to Re: Reading through a file and checking for a specific string
in thread Reading through a file and checking for a specific string
and@ARGV = glob '/export/home/date_file*';
is not exactly obvious and could lead to some serious head-scratching. Especially if the program grows a bit further and actually uses command line arguments. Same as above, with explicit file open / close.while ( my $line = <> ) {
#!/usr/bin/perl use warnings; use strict; use List::Util qw(sum); # Grab text files from archive directory with glob function my @files = glob '/export/home/date_file*'; my %counts = ( AB => 0, AC => 0, AD => 0, AE => 0, FG => 0, ); for my $file (@files) { open my $fh, '<', $file or die "$file: $!"; while ( my $line = <$fh> ) { my $var = substr $line, 41, 2; $counts{ $var }++ if exists $counts{ $var }; } close $fh or die "$file: $!"; } my $sum = sum values %counts; for my $type ( keys %counts ) { print "$type\t: $counts{$type}\n"; } print "Total \t: $sum\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 20, 2013 at 13:35 UTC | |
by poj (Abbot) on Aug 20, 2013 at 17:41 UTC | |
by vihar (Acolyte) on Aug 21, 2013 at 14:02 UTC | |
by soonix (Chancellor) on Aug 21, 2013 at 11:43 UTC | |
by vihar (Acolyte) on Aug 22, 2013 at 15:49 UTC | |
|
Re^3: Reading through a file and checking for a specific string
by vihar (Acolyte) on Aug 22, 2013 at 15:57 UTC | |
by vihar (Acolyte) on Aug 22, 2013 at 16:12 UTC |