in reply to Finding target unspecified value

If I understand your question correctly, you wish to find the largest element in an array. This is a fairly common task and the direct approach usually looks something like this:

use strict; use warnings; # you can initialize this any way you wish, this is proof of concept. my @source = ( 5, 53, 24, 38 ); my $maxseen = 0; my $seen_at = 0; my $cntr = 0; for ( @source ) { if ( $_ > $maxseen ) { $maxseen = $_; $seen_at = $cntr; } ++$cntr; }

No doubt, this is crude and someone will put up a one-liner using map or grep.

Update: changed it to have both the max element value and the position at which the max element value was first seen.

The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.

Replies are listed 'Best First'.
Re^2: Finding target unspecified value
by AnomalousMonk (Archbishop) on Oct 21, 2013 at 03:22 UTC

    boftx:
    Latnam doesn't generate any negative numbers in his or her  @source array, but if all the numbers were negative, the results would be spurious. List::Util::max and min don't have this problem (although they don't produce any index information; maybe see List::MoreUtils::first_index for this).

    >perl -wMstrict -le "my @source = ( -1, -2, -33, -4 ); ;; my $maxseen = 0; my $seen_at = 0; my $cntr = 0; for ( @source ) { if ( $_ > $maxseen ) { $maxseen = $_; $seen_at = $cntr; } ++$cntr; } ;; print qq{max $maxseen at index $seen_at}; " max 0 at index 0

      Point taken. :)

      The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.
Re^2: Finding target unspecified value
by Anonymous Monk on Oct 20, 2013 at 20:13 UTC
    How would I utilize what you have posted due to the script I am working with generates random numbers from 10 - 100. There are no set numbers to pick from

      Substitute @array for @source. Of course, Rolf demonstrates a more efficient solution using the max function from List::Util if all you want is the max value and not the location.

      The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.