Detect indexes of max and min values of an array





1
Date Submitted Mon. Jan. 18th, 2010 5:56 PM
Revision 1 of 1
Helper chorny
Tags Array | List | max | min | Perl
Comments 0 comments
Detect indexes of max and min values of an array in Perl

#Detect indexes of max and min values of an array in Perl
use strict;
use warnings;
my ($min,$max);
$min=$max=0;
my @list=(1,7,2,-2,7,0,-1);
foreach my $i (1..$#list) {
  $min=$i if $list[$i]<$list[$min];
  $max=$i if $list[$i]>$list[$max];
}
print "index max: $max\n";
print "index min: $min\n";

#(c) Alexandr Ciornii
 

Alexandr Ciornii

chorny.net
---
Sasha Chorny, http://chorny.net

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Helper chorny

Votes Down