... i can't figure out how to clear the foreach loop for every new set of data it uploads from a file.

The push built-in only adds elements to the end of an array (as you have discovered). Somewhere near the top of the main
    while (1) { ... }
loop and before reading the file into the array, add a
    @array = ();
statement to clear the array.

Better yet, IMHO: Because the  @array array is not used outside the while-loop in this code, only declare the array at the point at which it is needed. (The same applies to the  $average scalar.) Something like

my @array; while (my $data = <FH>) { push @array, $data; }
or more simply, forget the while-loop and just
my @array = <FH>;
Combined with the change to the  average() function that pryrt suggested, things should go more smoothly.

Update: Note that in the

foreach (@array) { chomp; if ($average > $_) { print "$_\t is below average.\n"; } elsif ($average < $_) { print "$_\t is above average.\n"; } elsif ($average = $_) { print "$_\t is equal to average.\n"; } }
loop of the OPed code, the  elsif ($average = $_) { ... } clause of the ladder of if-tests does not work as I expect you expect. The  $average = $_ expression assigns to  $average and does not test it. (Update: An assignment of 0 evaluates as 0, which is boolean false.) The result is that for an element of 0 in the array, the body of that clause is not executed (update: because an assignment of 0 evaluates to 0, which is boolean false):
c:\@Work\Perl\monks>perl -wMstrict -le "my @array = (-2, -1, 0, 1, 2); my $average = 0; ;; foreach (@array) { if ($average > $_) { print qq{$_\t is below average.}; } elsif ($average < $_) { print qq{$_\t is above average.}; } elsif ($average = $_) { print qq{$_\t is equal to average.}; } } " -2 is below average. -1 is below average. 1 is above average. 2 is above average.
I assume you meant  $average == $_ as the test, although at that point the variables can only be equal (i.e., neither is greater nor less than the other), so a plain
    else {
        print "... equal to ...";
        }
would have been sufficient.

And another thing...
The code posted by pryrt for the  average() subroutine returns 0 for an empty list of numbers passed to the function.

c:\@Work\Perl\monks>perl -wMstrict -le "print average(); ;; sub average { if (@_) { my @temp = @_; my $sum = 0; foreach (@temp) { $sum = $sum + $_; } return $sum/@temp; } } " 0
I think I would have elected to return undef or perhaps throw an exception, but this behavior is the option of the programmer. However, one should be aware of it.


Give a man a fish:  <%-{-{-{-<


In reply to Re: resetting a foreach loop! by AnomalousMonk
in thread resetting a foreach loop! by lunette

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.