diff options
| -rwxr-xr-x | challenge-032/duane-powell/perl5/ch-1.data.out | 11 | ||||
| -rwxr-xr-x | challenge-032/duane-powell/perl5/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-032/duane-powell/perl5/ch-2.pl | 75 | ||||
| -rwxr-xr-x | challenge-032/duane-powell/perl5/example1.txt | 31 | ||||
| -rwxr-xr-x | challenge-032/duane-powell/perl5/example2.txt | 33 |
5 files changed, 222 insertions, 0 deletions
diff --git a/challenge-032/duane-powell/perl5/ch-1.data.out b/challenge-032/duane-powell/perl5/ch-1.data.out new file mode 100755 index 0000000000..5a64178933 --- /dev/null +++ b/challenge-032/duane-powell/perl5/ch-1.data.out @@ -0,0 +1,11 @@ +{ + 'zucchini' => 16, + 'tomato' => 2, + 'cherry' => 4, + 'plum' => 4, + 'celery' => 14, + 'apple' => 6, + 'banana' => 2, + 'Broccoli' => 12, + 'kumquat' => 2 +} diff --git a/challenge-032/duane-powell/perl5/ch-1.pl b/challenge-032/duane-powell/perl5/ch-1.pl new file mode 100755 index 0000000000..2c1979f9f0 --- /dev/null +++ b/challenge-032/duane-powell/perl5/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw( say ); +use Data::Dumper; +$Data::Dumper::Terse = 1; +use Getopt::Long; + +# 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. +# For extra credit, add a -csv option to your script, which would generate, comma separated values. + +my $csv; +my @file; +GetOptions ( + "csv" => \$csv, + "file=s" => \@file, +); +usage() unless (@file); +$csv = ($csv) ? ',' : ''; + +my $href = {}; # hash ref to hold results +foreach (@file) { # iterate over all the user supplied files + open (my $fh, '<', $_) or do { warn "Can not open $_: $!"; next; }; + while (<$fh>) { + chomp; # remove newline char at end of string + $_ =~ s/[^a-zA-Z0-9]/ /g; # replace all non alpha chars with space + my @words = split(/ /, $_); # split on space filling an array with words + $href->{$_}++ foreach (@words); # count the word's occurance + } + close $fh; +} +# print out words in alphabetical order with counts +say "$_$csv $href->{$_}" foreach (sort {lc($a) cmp lc($b)} (keys %{$href})); + +# save out hash ref for ch-2.pl to use as input +my $file_name = 'ch-1.data.out'; +open(my $fh, '>', $file_name) or die "Could not open file '$file_name' $!"; +print $fh Dumper($href); +close $fh; + +sub usage { + print <<EOU; +Usage: +$0 -csv -f filename -f filename ... +$0 -csv -f example1.txt -f example2.txt +EOU + exit; +} + +__END__ + +./ch-1.pl -f example1.txt +apple 3 +banana 1 +Broccoli 6 +celery 6 +cherry 2 +kumquat 1 +plum 2 +tomato 1 +zucchini 8 + +./ch-1.pl -f example1.txt -f example2.txt -csv +apple, 6 +banana, 2 +Broccoli, 12 +celery, 14 +cherry, 4 +kumquat, 2 +plum, 4 +tomato, 2 +zucchini, 16 diff --git a/challenge-032/duane-powell/perl5/ch-2.pl b/challenge-032/duane-powell/perl5/ch-2.pl new file mode 100755 index 0000000000..f90a31938f --- /dev/null +++ b/challenge-032/duane-powell/perl5/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw( say ); + +# Write a function that takes a hashref where the keys are labels and the values are integer or floating point values. Generate a bar graph of the data and display it to stdout. +# If you fancy then please try this as well: (a) the function could let you specify whether the chart should be ordered by (1) the labels, or (2) the values + +use constant { + BLOCK => '####', +}; + +my $by_label = shift; + +# Eval the data ref saved out by ch-1.pl back into memory +my $data_file = 'ch-1.data.out'; +my $e; +if ( open( my $fh, $data_file ) ) { + $e .= $_ while (<$fh>); + close($fh); +} else { + die "Can not find '$data_file' $!"; +} +my $data = eval $e; + +generate_bar_graph($data); + +sub generate_bar_graph { + # find longest word and define $format for sprintf + my $longest = 0; + foreach (keys %{$_[0]}) { + my $l = length($_); + $longest = $l if ($l > $longest); + } + my $format = "%-$longest"."s"; + + # sort and print lines + if ($by_label) { + # sort by key + generate_bar($_[0],$_,$format) foreach (sort {lc($a) cmp lc($b)} (keys %{$_[0]})); + } else { + # sort by value + generate_bar($_[0],$_,$format) foreach (sort {$_[0]->{$a} <=> $_[0]->{$b}} (keys %{$_[0]})); + } +} + +sub generate_bar { + my $word = sprintf "$_[2]", $_[1]; + my $bar = BLOCK x $_[0]->{$_[1]}; + say "$word | $bar"; +} + +__END__ + +./ch-2.pl +banana | ######## +kumquat | ######## +tomato | ######## +cherry | ################ +plum | ################ +apple | ######################## +Broccoli | ################################################ +celery | ######################################################## +zucchini | ################################################################ + +./ch-2.pl 1 apple | ######################## +banana | ######## +Broccoli | ################################################ +celery | ######################################################## +cherry | ################ +kumquat | ######## +plum | ################ +tomato | ######## +zucchini | ################################################################ + diff --git a/challenge-032/duane-powell/perl5/example1.txt b/challenge-032/duane-powell/perl5/example1.txt new file mode 100755 index 0000000000..4413ed4732 --- /dev/null +++ b/challenge-032/duane-powell/perl5/example1.txt @@ -0,0 +1,31 @@ +apple +banana +apple +cherry +cherry +apple +kumquat +Broccoli +Broccoli +Broccoli +Broccoli +Broccoli +Broccoli +tomato +zucchini +zucchini +zucchini +zucchini +zucchini +zucchini +zucchini +zucchini +celery +celery +celery +celery +celery +celery +plum +plum + diff --git a/challenge-032/duane-powell/perl5/example2.txt b/challenge-032/duane-powell/perl5/example2.txt new file mode 100755 index 0000000000..3650a767a0 --- /dev/null +++ b/challenge-032/duane-powell/perl5/example2.txt @@ -0,0 +1,33 @@ +apple +banana +apple +cherry +cherry +apple +kumquat +Broccoli +Broccoli +Broccoli +Broccoli +Broccoli +Broccoli +tomato +zucchini +zucchini +zucchini +zucchini +zucchini +zucchini +zucchini +zucchini +celery +celery +celery +celery +celery +celery +celery +celery +plum +plum + |
