in reply to Re^5: du -h, sorted
in thread du -h, sorted
Very impressive. Made me waste a lot of time trying to beat it!
I think I've got it just a little shorter. I don't know if the time I spent on this has brought me any closer to enlightenment, but it did make me fast (as in skip a meal)...
The key is the 7 to the power of ord(char) bitwise 30 part. I had to write a little perl script that would find the magic values 7 and 30 which would work for a lexicographic (default) sort. That is, I take 7 to the power of those values, and subtract the value of $` ... to produce hash keys which sort properly.
Then I take a hash slice on the sorted result. The sort actually returns the entire flattened hash sorted, keys and values, but since the values in that hash don't map to anything, they are silent.
I think I need to go get something to eat.
I think I've got it just a little shorter. I don't know if the time I spent on this has brought me any closer to enlightenment, but it did make me fast (as in skip a meal)...
66 characters.perl -e'%h=map{/.\s/;7**(ord$&&30)-$`,$_}`du -h`;die @h{sort %h}'
The key is the 7 to the power of ord(char) bitwise 30 part. I had to write a little perl script that would find the magic values 7 and 30 which would work for a lexicographic (default) sort. That is, I take 7 to the power of those values, and subtract the value of $` ... to produce hash keys which sort properly.
Then I take a hash slice on the sorted result. The sort actually returns the entire flattened hash sorted, keys and values, but since the values in that hash don't map to anything, they are silent.
I think I need to go get something to eat.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: du -h, sorted
by hbm (Hermit) on Feb 26, 2009 at 19:49 UTC | |
by bellaire (Hermit) on Feb 26, 2009 at 20:10 UTC | |
by Lawliet (Curate) on Feb 27, 2009 at 00:48 UTC |
In Section
Cool Uses for Perl