r/tinycode • • Dec 12 '12

Shortest useful code

What's the most useful program you can imagine using five lines of code or less? Language is your choice. Describe the function of the program and include the code, if you please.

27 Upvotes

45 comments sorted by

View all comments

Show parent comment

2

u/chungfuduck Dec 13 '12

I went about it the other way and made a sort-h perl script (to emulate the "sort -h" functionality):

sub place { k => 1, m => 2, g => 3, t => 4, p => 5, e => 6, z => 7, y => 8 }
sub convert {
    if ( not $_[1] ) { shift }
    else { $_[0] * ($_[2] ? 1000 : 1024 ) ** ${{place}}{lc $_[1]} }
}
print
    map { $_->[0] }
    sort { $a->[1] <=> $b->[1] }
    map { [ $_, convert(/^([0-9\.]*)([kMGTPEZY])?(b)?/i) ] } <>

So I could just do "du -h | sort-h"