For one thing, you are ignoring the first element of the array ( Perl arrays are zero-based ), and comparing phantom elements at the end of the array. For example, if you have this array:
@stuff = ( 90, 5, 4 );
Your code will start with the second element, and compare the last element to a nonexistent ( and therefore undefined ) value (
$stuff[3] ).
For another thing, you are resetting the value of
$highest every time a following element has a lower value. With the code as it now exists,
$highest will always be the last element in the array. I urge you to work this out by hand with the short example.
Here is something that does what you want:
my $highest = 0;
foreach my $element ( @numbers ) {
$highest = $element if $element > $highest;
}
print $highest;
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.