Daren has asked for the wisdom of the Perl Monks concerning the following question:
Current script output.use strict; use warnings; my $string = 'Return code is'; my $filename = 'trace.Discovery'; my %errors =(); my $total = 0; if (open(my $fh, '<:encoding(UTF-8)', $filename)) { while (my $row = <$fh>) { chomp $row; if ($row =~ /\Q$string\E/) { my $digit = (split /\:/, $row)[-1]; $errors{$digit}++; } } } else { print "Could not open file '$filename'\n"; warn "This is the error --> $!"; } # First attempt to print keys foreach ( sort keys %errors ) { print "$_ - $errors{$_}\n"; $total = $total + $errors{$_}; } print "Total errors --> $total\n"; ### Another attempt to print the keys. while( my( $key, $value ) = each %errors ){ print "$key: $value\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can't get hash keys to print
by hippo (Archbishop) on Aug 24, 2015 at 14:19 UTC | |
by ikegami (Patriarch) on Aug 24, 2015 at 15:12 UTC | |
by Daren (Initiate) on Aug 24, 2015 at 14:39 UTC | |
|
Re: Can't get hash keys to print
by Athanasius (Archbishop) on Aug 24, 2015 at 14:45 UTC | |
by Daren (Initiate) on Aug 24, 2015 at 15:08 UTC | |
by Daren (Initiate) on Aug 24, 2015 at 18:41 UTC | |
by AnomalousMonk (Archbishop) on Aug 24, 2015 at 19:20 UTC | |
by stevieb (Canon) on Aug 24, 2015 at 19:11 UTC | |
by Daren (Initiate) on Aug 24, 2015 at 19:20 UTC |