TASK #1: Count Instances
Create a script that either reads standard input or one or more files specified
on the command-line. Count the number of times and then print a summary,
sorted by the count of each entry.
#!/usr/bin/perl use strict; use warnings; my %seen; while (<DATA>) { chomp; $seen{$_}++; } my $csv = defined $ARGV[0] && $ARGV[0] eq '-csv'; foreach my $fruit (sort { $seen{$b} <=> $seen{$a} } keys %seen) { $csv ? print("$fruit,$seen{$fruit}\n") : printf("%-10s %d\n", $fruit, $seen{$fruit}); } # Output: # apple 3 # cherry 2 # banana 1 # Output with -csv option: # apple,3 # cherry,2 # banana,1 __DATA__ apple banana apple cherry cherry apple