No such thing as a small change PerlMonks

Comment on

 Need Help??
But then I realized it'd probably be better to use Perl for the sorting, and got this shorter result

Now that sounds like a challenge...

1. The return is clearly superflous. Off with 7.
2. [0-9] can be replaced by \d, saving a character.
3. Hoist the exponentation into the hash values and use a bigger scaling factor (that may be written more concisely).
4. Replace $n and$u with $1 and$2 thus doing away with their initialisation.
5. Hang on, moving the exponentiation back into h() means that the hash declaration is shorter still.
6. If one considers that 'du -h|' adds to the overall count, then we can replace the diamond operator with a backtick of du -h. Overall this saves us the | of the pipe.
7. We can replace the fat commas with ordinary commas (and fortunately not receive warnings).
8. In fact, that regexp is way too verbose. /^(...)(.)/ will work just as well, saving a couple of characters.
9. update: I forgot to add the last step, changing shift to pop (since we only read one arg).

Which gives....

perl -e 'sub h{%h=(K,1,M,4,G,7);pop=~/^(...)(.)/&&$1**$h{$2}}print sort{h($b)<=>h($a)}du -h' ok, ok, I promise I'll leave it alone now, there's just the idea of using an anonymous hash to shave off a couple more characters, and the parens can be dropped from h($a), (but not h($b) because of precedences): perl -e 'sub h{pop=~/(...)(.)/&&$1**{K,1,M,4,G,7}->{$2}}print sort{h($b)<=>h\$a}du -h'

... 90 88 characters.

• another intruder with the mooring in the heart of the Perl

In reply to Re: du -h, sorted by grinder
in thread du -h, sorted by bellaire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file. [LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy [Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history LanX damns the cult of CB ;-) LanX WTF WTF WTF [LanX]: please forget my last 3 posts [LanX]: Yeah option a doesn't go into history [LanX]: probably I need to teach the app to restart after C-c Kill [Corion]: LanX: Maybe have an infinite-loop cmd file? Much easier than trying to manage that from within Perl IMO [Corion]: Alternatively, relaunch the application from cron (or a Windows cron) every minute

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2017-03-27 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.