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...
- The return is clearly superflous. Off with 7.
- [0-9] can be replaced by \d, saving a character.
- Hoist the exponentation into the hash values and use a bigger scaling factor (that may be written more concisely).
- Replace $n and $u with $1 and $2 thus doing away with their initialisation.
- Hang on, moving the exponentiation back into h() means that the hash declaration is shorter still.
- 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.
- We can replace the fat commas with ordinary commas (and fortunately not receive warnings).
- In fact, that regexp is way too verbose. /^(...)(.)/ will work just as well, saving a couple of characters.
- 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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|
|