P is for Practical PerlMonks

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

 on Jan 27, 2001 at 02:46 UTC Need Help??

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

Originally posted as a Categorized Question.

• Comment on How do I find the biggest number in an array of numbers.

Replies are listed 'Best First'.
Re: How do I find the biggest number in an array of numbers.
by Fastolfe (Vicar) on Jan 27, 2001 at 03:11 UTC
```use List::Util 'max';
...

my \$max = max( @list );
this logic might help sub max { my \$max = pop(@_); foreach (@_) { \$max = \$_ if \$_ > \$max; } \$max; } Shashidhar Iddamsetty
Re: How do I find the biggest number in an array of numbers.
by flocto (Pilgrim) on Jan 28, 2001 at 20:53 UTC
```my \$max = (sort { \$b <=> \$a } @array)[0];
Re: How do I find the biggest number in an array of numbers.
by hawson (Monk) on Jan 27, 2001 at 21:43 UTC
```map { \$max=\$_ if \$_>\$max } @list;

Originally posted as a Categorized Answer.

Re: How do I find the biggest number in an array of numbers.
by MrNobo1024 (Hermit) on Feb 05, 2001 at 21:36 UTC
```my \$max = \$list[0];
\$_ > \$max and \$max = \$_
for @list;
Re: How do I find the biggest number in an array of numbers.
by lemming (Priest) on Jan 27, 2001 at 02:59 UTC
Re: How do I find the biggest number in an array of numbers.
by Beatnik (Parson) on Jan 27, 2001 at 19:33 UTC
```@list = sort { \$a <=> \$b } @list;
\$max = \$list[\$#list];
Re: How do I find the biggest number in an array of numbers.
by Skeeve (Parson) on May 27, 2007 at 18:52 UTC
```my @data = (1, 30, 4, 5, 12, -12, 4.1, 18,20, 2, 0 ,4, 36, 0, 8, 15, 4

my \$max = \$data[0];
\$max= \$_>\$max ? \$_ : \$max foreach (@data);

print \$max,"\n"
Re: How do I find the biggest number in an array of numbers.
by salva (Canon) on May 27, 2007 at 11:26 UTC
If what you need is not just the biggest element but the N biggest elements, you can use Sort::Key::Top:
```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;

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

my @top5= (sort { \$b <=> \$a } @data)[0..\$n-1];

print join "\n",@top5,'';

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e
Re: How do I find the biggest number in an array of numbers.
by Anonymous Monk on Sep 26, 2004 at 16:22 UTC
0365478787

Originally posted as a Categorized Answer.

Re: How do I find the biggest number in an array of numbers.
by Anonymous Monk on Sep 05, 2003 at 15:33 UTC

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

Originally posted as a Categorized Answer.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54660]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-07-16 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.