diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2019-10-30 12:52:49 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2019-10-30 12:52:49 -0400 |
| commit | 845ea55cef4f18aa97907ca20128649c8b2b3d9a (patch) | |
| tree | 533db92d49b395069eb0449091a2c8768ceef71d /challenge-032 | |
| parent | 8b8f31ae1cd3bafe336df787e5a79b3a34730647 (diff) | |
| download | perlweeklychallenge-club-845ea55cef4f18aa97907ca20128649c8b2b3d9a.tar.gz perlweeklychallenge-club-845ea55cef4f18aa97907ca20128649c8b2b3d9a.tar.bz2 perlweeklychallenge-club-845ea55cef4f18aa97907ca20128649c8b2b3d9a.zip | |
Challenge 32!
Diffstat (limited to 'challenge-032')
| -rwxr-xr-x | challenge-032/dave-jacoby/perl5/ch-1.pl | 66 | ||||
| -rwxr-xr-x | challenge-032/dave-jacoby/perl5/ch-2.pl | 65 | ||||
| -rw-r--r-- | challenge-032/dave-jacoby/perl5/example.txt | 6 |
3 files changed, 137 insertions, 0 deletions
diff --git a/challenge-032/dave-jacoby/perl5/ch-1.pl b/challenge-032/dave-jacoby/perl5/ch-1.pl new file mode 100755 index 0000000000..bfe9cc9aa6 --- /dev/null +++ b/challenge-032/dave-jacoby/perl5/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl + +# 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. + +# So with the following input in file example.txt + +# apple +# banana +# apple +# cherry +# cherry +# apple + +# the script would display something like: + +# apple 3 +# cherry 2 +# banana 1 + +# For extra credit, add a -csv option to your script, which would generate: + +# apple,3 +# cherry,2 +# banana,1 + +use strict; +use warnings; +use feature qw{ say }; + +use Getopt::Long; +my $csv = 0; +GetOptions( 'csv' => \$csv, ); + +# all the arguments are in ARGV, except we've removed the -csv flag with +# GetOptions, we just do a file test to make sure we can open them before +# opening them. We COULD test for repeated files, but we don't. + +# and we use the hash to keep a count, rather than store as an array +# and count later. + +my %lines; +if ( scalar @ARGV ) { + for my $arg (@ARGV) { + if ( -f $arg && open my $fh, '<', $arg ) { + while (<$fh>) { + chomp; + $lines{$_}++; + } + } + } +} +else { + while (<STDIN>) { + chomp; + $lines{$_}++; + } +} + +my $separator = $csv ? ',' : "\t"; +for my $k ( sort { $lines{$b} <=> $lines{$a} } keys %lines ) { + my $v = $lines{$k}; + say join $separator, $k, $v; +} diff --git a/challenge-032/dave-jacoby/perl5/ch-2.pl b/challenge-032/dave-jacoby/perl5/ch-2.pl new file mode 100755 index 0000000000..8938318b05 --- /dev/null +++ b/challenge-032/dave-jacoby/perl5/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ say state }; +use experimental qw{ postderef signatures switch }; + +# Task #2 - ASCII bar chart + +# 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. + +# The input could be something like: + +# $data = { apple => 3, cherry => 2, banana => 1 }; +# generate_bar_graph($data); + +# And would then generate something like this: + +# apple | ############ +# cherry | ######## +# banana | #### + +# 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 List::Util qw{max}; +use Getopt::Long; + +my $values = 0; +GetOptions( 'values' => \$values, ); +my $order = $values ? 'values' : 'label'; +my $data = { apple => 3, cherry => 2, banana => 1 }; + +generate_bar_graph( $data, $order ); + +exit; + +# I am not the happiest with this code, because the length of the +# bar is hardcoded to be 4 times the number given. If data was to show +# the vote tally for even a small local election, the numbers would +# expand FAR past the size of the terminal. But, given the data we have, +# it works. + +# example used left-padding for the key names, and I replicated that. +# "string x number of repeats" is cool and useful + +sub generate_bar_graph ( $data, $order = 'label' ) { + my @keys = sort keys $data->%*; + @keys = sort { $data->{$b} <=> $data->{$a} } keys $data->%* + if $order eq 'values'; + + my $max = 4 + max map { length $_ } @keys; + + for my $k (@keys) { + my $v = 0 + $data->{$k}; + my $pad = ' ' x ( $max - length $k ); + say join '' , $pad, $k, ' | ', '#' x ( 4 * $v ); + } +} diff --git a/challenge-032/dave-jacoby/perl5/example.txt b/challenge-032/dave-jacoby/perl5/example.txt new file mode 100644 index 0000000000..00fe021a08 --- /dev/null +++ b/challenge-032/dave-jacoby/perl5/example.txt @@ -0,0 +1,6 @@ +apple +banana +apple +cherry +cherry +apple |
