Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

( #54660=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jan 27, 2001 at 02:46 UTC
Q&A  > arrays


Description:

Answer: How do I find the biggest number in an array of numbers.
contributed by Fastolfe

use List::Util 'max'; ... my $max = max( @list );
Answer: How do I find the biggest number in an array of numbers.
contributed by flocto

my $max = (sort { $b <=> $a } @array)[0];
Answer: How do I find the biggest number in an array of numbers.
contributed by MrNobo1024

my $max = $list[0]; $_ > $max and $max = $_ for @list;
Answer: How do I find the biggest number in an array of numbers.
contributed by lemming

See finding min and max of array recursivly. (Nevermind the recursive bit.)

Answer: How do I find the biggest number in an array of numbers.
contributed by Beatnik

@list = sort { $a <=> $b } @list; $max = $list[$#list];
Answer: How do I find the biggest number in an array of numbers.
contributed by Skeeve

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"
Answer: How do I find the biggest number in an array of numbers.
contributed by salva

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;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (14)
    As of 2015-07-30 14:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (271 votes), past polls