diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-10-28 15:47:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-28 15:47:52 +0000 |
| commit | 6c51f7a48e689b56363be56ee14f2eddec045576 (patch) | |
| tree | c59b870206f228488bb76b18c52aa4210edffc7c | |
| parent | 20f13bc3f77dcc532de961cbd0bb470fb8625d66 (diff) | |
| parent | 08cfab0f396f33e71a5007824895f11580828448 (diff) | |
| download | perlweeklychallenge-club-6c51f7a48e689b56363be56ee14f2eddec045576.tar.gz perlweeklychallenge-club-6c51f7a48e689b56363be56ee14f2eddec045576.tar.bz2 perlweeklychallenge-club-6c51f7a48e689b56363be56ee14f2eddec045576.zip | |
Merge pull request #861 from Doomtrain14/master
Update solution for task#1
| -rw-r--r-- | challenge-032/yet-ebreo/perl5/ch-1.pl | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/challenge-032/yet-ebreo/perl5/ch-1.pl b/challenge-032/yet-ebreo/perl5/ch-1.pl index c34b7be4cf..33edba3f0d 100644 --- a/challenge-032/yet-ebreo/perl5/ch-1.pl +++ b/challenge-032/yet-ebreo/perl5/ch-1.pl @@ -23,6 +23,7 @@ use feature 'say'; my @files; my $csv_flag = 0; +my $longest = 0; for my $arg (@ARGV) { if ($arg eq '-csv') { $csv_flag = 1; @@ -30,11 +31,13 @@ for my $arg (@ARGV) { 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; + $longest = length $word if $longest < length $word; $dictionary{$word}++; } } else { @@ -42,17 +45,42 @@ if (@ARGV < ($csv_flag+1)) { open my $handle, '<', $text; chomp(my @lines = <$handle>); close $handle; - + for my $word (@lines) { + $longest = length $word if $longest < length $word; $dictionary{$word}++; } } } +$longest = 0 if $csv_flag; + for my $keys (sort { $dictionary{$b}-$dictionary{$a} } sort keys %dictionary) { - say $keys.($csv_flag?",":"\t").$dictionary{$keys}; + printf ("%-${longest}s%s%s\n",$keys,$csv_flag?",":" ",$dictionary{$keys}); } =begin +perl .\ch-1.pl +Type the word(s) then press enter, :end to quit: +a +quick +brown +apple +jumps +over +dog +brown +cat +apple +:end +apple 2 +brown 2 +a 1 +cat 1 +dog 1 +jumps 1 +over 1 +quick 1 + perl .\ch-1.pl .\sample.txt apple 2 brown 2 |
