Example 8-10. examples/arrays/count_digits.txt
23 34 9512341 3 34 2452345 5353 67 22 42136357013412 42 5 65 64
Example 8-11. examples/arrays/count_digits.pl
#!/usr/bin/perl
use strict;
use warnings;
my $filename = shift or die "Usage: $0 filename\n";
my @count;
open(my $fh, "<", $filename)
or die "Could not open '$filename': $!";
while (my $line = <$fh>) {
chomp $line;
my @chars = split "", $line;
foreach my $c (@chars) {
if ($c ne " ") {
$count[$c]++;
}
}
}
foreach my $i (0..9) {
print "$i ", ($count[$i] ? $count[$i] : 0), "\n";
}
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| Ternary operator | Up | $_ |