From joshua.mcadams at gmail.com Thu Jun 1 08:13:59 2006 From: joshua.mcadams at gmail.com (Joshua McAdams) Date: Thu, 1 Jun 2006 11:13:59 -0400 Subject: [Classiccity-pm] YAPC::NA Message-ID: <49d805d70606010813r41631e5arddf7f1e4a4cae8e0@mail.gmail.com> Hi there fellow Perl Mongers. I'm writing to remind you all that YAPC::NA is only a few weeks away. The conference will be held in Chicago June 26th through 28th and will feature four simultaneous sessions of Perl talks for three days in addition to a job fair, banquet, and auction. After the conference Damian Conway, Randal Schwartz, and brian d foy will be sticking around and conducting professional training classes and extremely reduced prices. This email is a little spammy (sorry about that), but I just wanted to remind you all about the conference and also ask for your help in promoting it so that we can fill up the few spots that are remaining. For more information check out http://www.yapcchicago.org. We invite you to put up posters: http://yapcchicago.org/yapc_poster.pdf http://yapcchicago.org/yapc_poster_white.pdf Or maybe a web banner: http://www.yapcchicago.org/yapc_banner_wide.jpg http://www.yapcchicago.org/yapc_banner_narrow.jpg Thank you for your help in making YAPC a success once again, Josh McAdams From lindadl at uga.edu Mon Jun 5 11:02:16 2006 From: lindadl at uga.edu (Linda Dubes Law) Date: Mon, 5 Jun 2006 14:02:16 -0400 Subject: [Classiccity-pm] Greatest number Message-ID: <20060605140216890.00000002976@lindadl> I am trying to figure out an efficient way to get the greatest number in a file. The file is rather large. Should I do something like: ********************* while () { chomp; ($cardnumber) = (split / /) [1]; if ($cardnumber gt $largest) { $largest = $cardnumber ********************* Or should I read everything in an array and then sort the array Or should I go to the system, sort the file and then is there some way to read in only the last record of a file? Or perhaps sort descending and then read in only the first record? As always, your input is appreciated! Thanks, Linda Law From joshua.mcadams at gmail.com Mon Jun 5 11:12:19 2006 From: joshua.mcadams at gmail.com (Joshua McAdams) Date: Mon, 5 Jun 2006 14:12:19 -0400 Subject: [Classiccity-pm] Greatest number In-Reply-To: <20060605140216890.00000002976@lindadl> References: <20060605140216890.00000002976@lindadl> Message-ID: <49d805d70606051112h15a68e37h4b78f28ab1a4e864@mail.gmail.com> > Or should I read everything in an array and then sort the array I would think that if file size might be an issue, then you'd probably want to stick with what you are doing now and not load the entire thing into memory. Just to be sure, you know that you're doing a string compare on the numbers instead of a numeric compare? Probably not too big of a deal and maybe even what you intended... just thought I'd call it out. From pkeck at uga.edu Mon Jun 5 11:22:08 2006 From: pkeck at uga.edu (Paul Keck) Date: Mon, 5 Jun 2006 14:22:08 -0400 Subject: [Classiccity-pm] Greatest number In-Reply-To: <20060605140216890.00000002976@lindadl> References: <20060605140216890.00000002976@lindadl> Message-ID: <20060605182208.GI4200@uga.edu> On Mon, Jun 05, 2006 at 02:02:16PM -0400, Linda Dubes Law wrote: > Or should I go to the system, sort the file and then is there some way to > read in only the last record of a file? Or perhaps sort descending and > then read in only the first record? tail -1 head -1 will give you the head and tail line. I'd try running it like system("cat filename | sort -n +3 -4 |tail -1") and adjust the sort until it sorted on the proper field. See how long that takes compared to using perl. If you need to do it in perl alone, then the code you put in looked pretty good to me. No need to store the lines in an array if that's all you want out of there. If you're looking at making multiple passes through it, I suggest sucking it into a hash. -- Paul Keck pkeck at uga.edu http://www.arches.uga.edu/~pkeck University of Georgia http://www.uga.edu/ucns/telecom EITS Network Engineering mailto:pkeck at ediacara.org --Opinions mine.-- Go fighting anomalocaridids!!! From dwestbrook at tungstenlearning.com Mon Jun 5 12:22:48 2006 From: dwestbrook at tungstenlearning.com (David Westbrook) Date: Mon, 05 Jun 2006 15:22:48 -0400 Subject: [Classiccity-pm] Greatest number In-Reply-To: <20060605182208.GI4200@uga.edu> References: <20060605140216890.00000002976@lindadl> <20060605182208.GI4200@uga.edu> Message-ID: <44848488.9060003@tungstenlearning.com> 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