aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2019-10-28 23:02:50 +0800
committerYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2019-10-28 23:02:50 +0800
commit2233bf4b1c5a282095ae34b4bfb277a8b6da0f58 (patch)
treee541a00a2cc6385ec71f15bb3c130665183f3f0e
parent4ea534b8afcaa524e258936ac5eae51eaa5a2cc9 (diff)
downloadperlweeklychallenge-club-2233bf4b1c5a282095ae34b4bfb277a8b6da0f58.tar.gz
perlweeklychallenge-club-2233bf4b1c5a282095ae34b4bfb277a8b6da0f58.tar.bz2
perlweeklychallenge-club-2233bf4b1c5a282095ae34b4bfb277a8b6da0f58.zip
Added perl5 solution ch#32-1
-rw-r--r--challenge-032/yet-ebreo/perl5/ch-1.pl87
-rw-r--r--challenge-032/yet-ebreo/perl5/sample.txt17
2 files changed, 104 insertions, 0 deletions
diff --git a/challenge-032/yet-ebreo/perl5/ch-1.pl b/challenge-032/yet-ebreo/perl5/ch-1.pl
new file mode 100644
index 0000000000..c34b7be4cf
--- /dev/null
+++ b/challenge-032/yet-ebreo/perl5/ch-1.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+# 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 'say';
+
+my @files;
+my $csv_flag = 0;
+for my $arg (@ARGV) {
+ if ($arg eq '-csv') {
+ $csv_flag = 1;
+ } else {
+ push @files, $arg;
+ }
+}
+my %dictionary;
+if (@ARGV < ($csv_flag+1)) {
+ say "Type the word(s) then press enter, :end to quit:";
+ while (chomp(my $word = <STDIN>)) {
+ ($word =~ /^:end$/) && last;
+ $dictionary{$word}++;
+ }
+} else {
+ for my $text (@files) {
+ open my $handle, '<', $text;
+ chomp(my @lines = <$handle>);
+ close $handle;
+
+ for my $word (@lines) {
+ $dictionary{$word}++;
+ }
+ }
+}
+
+for my $keys (sort { $dictionary{$b}-$dictionary{$a} } sort keys %dictionary) {
+ say $keys.($csv_flag?",":"\t").$dictionary{$keys};
+}
+=begin
+perl .\ch-1.pl .\sample.txt
+apple 2
+brown 2
+dog 2
+a 1
+banana 1
+black 1
+cat 1
+fox 1
+jumps 1
+lazy 1
+over 1
+quick 1
+test 1
+the 1
+
+perl .\ch-1.pl -csv .\sample.txt
+apple,2
+brown,2
+dog,2
+a,1
+banana,1
+black,1
+cat,1
+fox,1
+jumps,1
+lazy,1
+over,1
+quick,1
+test,1
+the,1
+=cut \ No newline at end of file
diff --git a/challenge-032/yet-ebreo/perl5/sample.txt b/challenge-032/yet-ebreo/perl5/sample.txt
new file mode 100644
index 0000000000..b702ef5428
--- /dev/null
+++ b/challenge-032/yet-ebreo/perl5/sample.txt
@@ -0,0 +1,17 @@
+a
+quick
+brown
+fox
+jumps
+over
+the
+lazy
+dog
+test
+apple
+banana
+black
+brown
+apple
+dog
+cat \ No newline at end of file