Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: ⭐ (arrays)

Originally posted as a Categorized Question.

use List::Util 'max'; ... my $max = max( @list ); [download]

my $max = (sort { $b <=> $a } @array)[0]; [download]

map { $max=$_ if $_>$max } @list; [download]

Originally posted as a Categorized Answer.

my $max = $list[0]; $_ > $max and $max = $_ for @list; [download]

@list = sort { $a <=> $b } @list; $max = $list[$#list]; [download]

my @data = (1, 30, 4, 5, 12, -12, 4.1, 18,20, 2, 0 ,4, 36, 0, 8, 15, 4 +2, 4711, 0xdead, 0xbeef); my $max = $data[0]; $max= $_>$max ? $_ : $max foreach (@data); print $max,"\n" [download]

use Sort::Key::Top qw(rntop ntop); my $n = 5; my @data = (1, 30, 4, 5, 12, -12, 4.1, ...); my @min_n = ntop $n => @data; my @max_n = rntop $n => @data; [download]

I didn't look at the module, but tell me: What's wrong with this:

my $n = 5; my @data = (1, 30, 4, 5, 12, -12, 4.1, 18,20, 2, 0 ,4, 36, 0, 8, 15, 4 +2, 4711, 0xdead, 0xbeef); my @top5= (sort { $b <=> $a } @data)[0..$n-1]; print join "\n",@top5,''; [download]

Re: How do I find the biggest number in an array of numbers.

www.com | www.net | www.org

No recent polls found

Comment onHow do I find the biggest number in an array of numbers.