note
dcmertens
I disagree. This is kinda clever, but I suspect that a single pass would be clearer. I would do this:
<code>
my @array = qw(4 3 7 8 9 49 12 23 43);
my ($largest, $largest_idx) = ($array1[0], 0);
for my $i (1 .. $#array) {
($largest, $largest_idx) = ($array[$i], $i)
if $largest < $array[$i];
}
print "Largest number is $largest, at offset $largest_idx\n";
</code>
It really seems like something like this should be available in [metamod://List::Util] or [metamod://List::MoreUtils], and I would use one of those instead of the sort+smartmatch. At any rate, I am most likely to need this sort of calculation in PDL, where I would do it like this:
<code>
use PDL;
my $data = pdl(qw(4 3 7 8 9 49 12 23 43));
my $largest_idx = $data->maximum_ind;
my $largest = $data->at($max_idx);
print "Largest number is $largest, at offset $largest_idx\n";
</code>
but PDL would be overkill for a dataset of this size.
62913
1070730