Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: Golf: Improve this guy's fail . . . please!

by ikegami (Pope)
on Jun 30, 2009 at 22:44 UTC ( #776206=note: print w/replies, xml ) Need Help??

in reply to Re: Golf: Improve this guy's fail . . . please!
in thread Golf: Improve this guy's fail . . . please!

At the opposite end of the spectrum, reading in the whole file:

local $/; ++$chars{$_} for <> =~ /\w/g

(Trimmed: local$/;++$c{$_}for<>=~/\w/g)

It only accepts one file, though.

Update: 5 shorter, accepts multiple files, and doesn't mess with $/:

++$chars{$_} for map /\w/g, <>

(Trimmed: ++$c{$_}for map/\w/g,<>)

Replies are listed 'Best First'.
Re^3: Golf: Improve this guy's fail . . . please!
by mr_mischief (Monsignor) on Jul 01, 2009 at 00:38 UTC
    I borrowed your idea and made something short and very ugly which does the whole job including percentages and total. I even added a count of distinct characters:
    ++$t,++$c{$_}for map/\w/g,<>; print$_,"\t",$c{$_},"\t",$c{$_}/$t*100,$/for sort keys %c; print"Distinct: ".keys(%c)."\tTotal: $t\n";

    I doubt I'd call that an "improvement", at least with a straight face. It could be cleaned up pretty easily, though.

      It could be cleaned up pretty easily, though.

      Then do it! No good leaving lousy code floating around... ;-)

        But... but... it's golf! ;-)

        I guess adding a feature's not exactly in the spirit of golf, though. More golfed:

        ++$t,++$c{$_}for map/\w/g,<>; print$_,' ',$c{$_},' ',$c{$_}/$t*100,$/for sort keys%c; print"Total: $t\n"

        So now that that's out of the way, how about still short but somewhat easier to read?

        ++$t, ++$c{$_} for map /\w/g, <>; for ( sort keys %c ) { $p = sprintf '%3.2f', $c{$_} / $t * 100; printf "%-5s%5d%7s%%\n", $_, $c{$_}, $p; } print 'Distinct: ' . (scalar keys %c) . "\tTotal: $t\n"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://776206]
[ambrus]: No, I think the timers are handled fine in the event loop, as far as I can see.
[Corion]: So far, I liked Prima quite well as it simply seems to do what I want even if it has an upside-down coordinate system. But it installs from CPAN out of the box and is cross-platform
[ambrus]: Corion: which coordinate system is the upside down one?
[Corion]: ambrus: "the other" ;) To me, "normal" for GUIs means (0,0) is left-upper corner of the monitor/window. But for Prima, the lower-left corner it is ;)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2016-12-09 10:39 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.