I'm not quite sure what you want here. The numeric sort and your question implies that you want the max value? If that is what you want:
#!/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
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;
# 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
And yes, if 90 appeared twice, this changes things as both of these would be the same.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.