[Classiccity-pm] Greatest number

David Westbrook dwestbrook at tungstenlearning.com
Mon Jun 5 12:22:48 PDT 2006


Paul Keck wrote:

> system("cat filename | sort -n +3 -4 |tail -1")

That's a "useless use of cat" :)    
http://laku19.adsl.netsonic.fi/%7Eera/unix/award.html

What do the "+3 -4" to sort do?

A blind guess might be that "head -1" is faster than "tail -1" on a pipe 
so it doesn't have to read to the end (unless it has someway to seek to 
the end), so using sort's "-r" option might increase it a little.
Also need backticks instead of system() to get the output ..
And i'll throw in an awk since OP wanted 2nd column (could use cut if 
the actual delim was known):

my $max_from_shellout = `awk '{print $2}' | sort -rn $filename | head -1`


Cool -- after a "man awk" (i've only ever used it in a "cut" fashion), 
it can sort as well:
  awk '{ print $1 | "sort" }' /tmp/n | tail -1


A pure-perl (and possibly overkill, but maybe not -- no idea of context 
here, but maybe this would be a good way for OP to access/update the 
file in question) way would be to use Class::DBI and DBD::CSV to set up 
a db object wrapper class for this "table" (file), and just use the 
maximum_value_of() method.
package My::Sample;
use base 'Class::DBI';
__PACKAGE__->connection("DBI:CSV:f_dir=data/");
__PACKAGE__->table('player_changes');
__PACKAGE__->columns( All => qw/ col1 col2 foo bar / );
############
in code:   my $max = My::Sample->maximum_value_of('col2');
(maybe not the most effiicient either, but fun, right? ;) )

--david



More information about the Classiccity-pm mailing list