my @average = $num_list_total / $size; print "The average of the numbers is @average.\n";

This is a bug waiting to happen. $average is a scalar variable containing a single value. @average is an array variable containing a list of values. You're getting away with it here because the first line puts the result of the division in the first element of @average, and then the print statement prints out the whole array, which happens to have only that one element. But if you assigned or used the value a bit differently, it could easily break. Use scalar variables for single values.

@average = average(@_);

Same problem here, assigning what's likely a single value to an array (which will be a problem in a few lines). You're also calling a subroutine named average() which doesn't exist in your script, which means your script doesn't run at all. Have you actually tried running it as it exists here?

Anyway, that line assigns the result of the average() call to the first element of @average. Which means that this line doesn't do what you want:

    if ($element > @average) {

Since this evaluates @average in scalar context, it uses the number of elements in @average, not the value(s) stored in it. Since you (presumably) stored a single value in it, this has the effect of:

    if($element > 1){

So again, store your single values in scalar variables. That's a bigger problem than whatever confusion you may be causing by localizing the same variables inside and outside your subroutine.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re: Pure variable confusion! by aaron_baugher
in thread Pure variable confusion! by jmvbxx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &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.