4 ms·
Would perl -lane '$c{lc $_}++ for @F;END{print "$_ $c{$_}" for sort {$c{$b}<=>$c{$a}||$a cmp $b} keys %c}' < input.txt work?
by mhd 6y ago
Would
perl -lane '$c{lc $_}++ for @F;END{print "$_ $c{$_}" for sort {$c{$b}<=>$c{$a}||$a cmp $b} keys %c}' < input.txt
work?
- deleted 6y ago[deleted]
- jlg23 6y agoIf you interviewed for a perl job, you'd probably be disqualified for writing code that is too readable ;)
- anthk 6y agoPerl is readable, just indent it.
- nerdponx 6y agoWhat is the readable indented version of this? perl -anle '$w{lc s/[[:punct:]]//r}++ foreach @F; END{print map{"$w{$_} $_ \n"} (sort{$w{$a} <=> $w{$b}} keys(%w))}'
- faizshah 6y agoI wrote that as a one liner it’s not supposed to be readable. Here’s a readable version of essentially the same program: https://perlmaven.com/count-words-in-text-using-perl https://perlmaven.com/count-words-in-text-using-perl
- jlg23 6y agoI know, I still have my black belt in perl somewhere. The one-liner is readable, too. Throw in some needless array-referencing-and-dereferencing so people can rightfully consider perl a write-only language...
- mhd 6y agoI know this is meant as a joke, but I just use this to once again note that Perl actually had a very common linter/pretty printer way before it became all the rage (perltidy/perlcritic). I also wish other languages had a book like Perl Best Practices, that goes beyond just syntactical issues. To be fair, I once came across a sentiment that was pretty close to "readability is bad". Taken from both the (bourne) shell and awk, perl has lots of short variables that determine parsing input and other matters, like "$/". You can then `use English` to have alternative names for that, but those might not be as well known ("$/" would be "$INPUT_RECORD_SEPARATOR", or "$RS" as a tribute to awk). So the line noise might be more common and thus more understandable than the "readable" version. I don't quite buy that for e.g. "$INPUT_RECORD_SEPARATOR", but there's an argument to be made for "$RS" or "$ARG" being worse than the Asterix swear words.
- jlg23 6y ago> I know this is meant as a joke, but I just use this to once again note that Perl actually had a very common linter/pretty printer way before it became all the rage (perltidy/perlcritic). And all great perl hackers deliver readable code. But we still enjoy a round of perl golf once in a while. And honor demands, that, if you respond in perl golf style to an interview question, you make it count ;)
- faizshah 6y agoNice! I came up with: perl -nle '$w{lc s/[[:punct:]]//r}++ foreach split(/\s+/,$_); print map{"$w{$_} $_ \n"} (sort{$w{$a} <=> $w{$b}} keys(%w)) if eof()' < kjvbible.txt If anyone's curious this takes .47 seconds on my local machine whereas the optimized shell solution takes about 3.5 seconds. Edit: It looks like mhd's usage of autosplit -a and @F are faster than split($_) but calling print foreach key is slower than my print map strategy. So our solutions are combined to get: perl -anle '$w{lc s/[[:punct:]]//r}++ foreach @F; END{print map{"$w{$_} $_ \n"} (sort{$w{$a} <=> $w{$b}} keys(%w))}' < kjvbible.txt Which runs in .39 seconds. mhd's solution runs in .42 seconds. For reference the optimized C version takes .059 seconds (possibly cause of clang?), wc -w takes .023s and the optimized python version takes .288
- FireTrader 6y agoI haven't written perl in 5 years. Reading this evokes a rare sense of joy / Stockholm syndrome.