in reply to not able to get last index of array
Update: I thought that I should point out that you can re-assign the sorted array back to the same variable....#!/usr/bin/perl use strict; use warnings; use List::Util qw(max); #CORE nothing to install # changed the order to make max value # in the middle of @numbers my @numbers = (15,5,7,3,90,9,1,20,13,9,8, 15,16,2,6,12); print "max is =", max(@numbers), "\n"; __END__ max is =90
And yes, if 90 appeared twice, this changes things as both of these would be the same.#!/usr/bin/perl use strict; use warnings; # changed the order to make max value # in the middle of @numbers my @numbers = (15,5,7,3,90,9,1,20,13,9,8, 15,16,2,6,12); @numbers = sort {$a <=> $b} @numbers; print "max = $numbers[-1]\n"; print "next to max = $numbers[-2]\n"; __END__ max = 90 next to max = 20
|
|---|