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";
In reply to Re^2: Reading through a file and checking for a specific string
by Monk::Thomas
in thread Reading through a file and checking for a specific string
by vihar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |