in reply to I need to sort out some values and get only the highest generated by my perl script

Without really digging that deep into your code, the task of finding the largest value is usually best handled by (pseudocode):
my ($largest_index, $largest_value) = (-1E6, undef); #make sure $largest_index starts below the lowest possible #value you can get for my $data (@some_big_long_list){ my $this_index = index_from($data); if ($this_index > $largest_index){ $largest_index = $this_index; $largest_value = $data; } } #do something with largest data
That is, just keep track of what the largest you've seen so far is as you're going through them, and when you reach the end, it's guaranteed to be the largest overall.
  • Comment on Re: I need to sort out some values and get only the highest generated by my perl script
  • Download Code

Replies are listed 'Best First'.
Re^2: I need to sort out some values and get only the highest generated by my perl script
by riffraff (Pilgrim) on Oct 29, 2004 at 15:29 UTC
    If all he is doing is printing the largest value, would not the following suffice?

    program.pl|sort -n|tail -1

    No extra coding needed, and it uses programs that already exist on the system.

      If that's what's happening, yes. I read his message as "I have a loop that goes 1000 times, and each time it generates a bunch of data. I want the largest value produced from each run of the loop, ie, 1000 largest values," which wouldn't work from your model. Also, it's almost certain to be less efficient (O(n log n) instead of O(n) to do the sort, then throw away all the extra sort data that was just computed).