in reply to du -h, sorted
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
Re^2: du -h, sorted
by Roy Johnson (Monsignor) on Feb 25, 2009 at 22:58 UTC
|
Are you sure yours gets correct results? I'm getting 372K between 4.4M and 3.6M.
perl -e 'sub c{pop=~/[GMK]/;ord($&)&7}print sort{c($b)<=>c($a)or$b<=>$
+a}`du -h`'
Caution: Contents may have been coded under pressure.
| [reply] [d/l] |
|
perl -e'sub h{pop=~/(...)(.)/&&{M,1e3,G,1e6}->{$2}+$1}print sort{h($b)
+<=>h$a}`du -h`'
update: No wait! There's something in your idea of bitanding... Just have to get rid of the second spaceship comparator...
perl -e'sub c{pop=~/[GMK]/;(ord$&&7).1e3+$`}print sort{c($b)<=>c$a}`du
+ -h`'
76 strokes.
• another intruder with the mooring in the heart of the Perl
| [reply] [d/l] [select] |
|
perl -e 'sub h{pop=~/^([\d.]+)(.)/&&{K,1e1,M,1e3,G,1e6}->{$2}+$1}print
+ sort{h($b)<=>h$a}`du -h`'
For the second one, I think you're in a bind because ord(k)&7 and ord(K)&7 are equal...
And I offer another method, bumming heavily from yours, longer but possibly faster on large filesystems:
perl -e 'print map substr($_,8),reverse sort map sprintf("%8d",(/^([\d
+.]+)([kKMG])/)?{K,1e1,M,1e3,G,1e6}->{$2}+$1:$1).$_,`du -h`'
| [reply] [d/l] [select] |
|
Oh, I see. Could I then also change the regex to save two more characters?
perl -e'sub c{pop=~/.\s/;(ord$&&7).1e3+$`}print sort{c($b)<=>c$a}`du -
+h`'
74 characters.
And, in fact, since we are only using .1e3 to make sure the sizes sort properly, why not save one more:
perl -e'sub c{pop=~/.\s/;(ord$&&7)**7+$`}print sort{c($b)<=>c$a}`du -h
+`'
73 characters. | [reply] [d/l] [select] |
|
|
|
Re^2: du -h, sorted
by hbm (Hermit) on Feb 25, 2009 at 23:09 UTC
|
I like your subtle omission of parens from h$a, versus h($b). But to sort correctly, I tweaked slightly to use the OP's 10,20,30 values.
perl -e 'sub h{pop=~/(...)(.)/&&$1*2**{K,10,M,20,G,30}->{$2}}print sor
+t{h($b)<=>h$a}`du -h`'
| [reply] [d/l] |
|
|