hello, monks! it's been a little while, and i need some help with a small thing. i've figured out how to make this entire thing run, but i can't figure out how to clear the foreach loop for every new set of data it uploads from a file. the explicit package name for "@array" can't be moved into the loop because the subroutine below needs it as well, which seemed to be what everyone said would fix the problem.

is there any other way to clear the data each time? i've tried 'next', 'last', and 'redo', and none of them seem to do anything besides mess the loop up entirely. i've also tried adding another array into the loop using the original array to manipulate the data into clearing that way, but as i thought, that did nothing.

does anyone have any simple way of getting the data to clear for each loop? i'm sure it's something simple that i'm just overlooking. here's the code itself:

#!usr/bin/perl use strict; use diagnostics; use warnings; my @array; my $average; while (1) { print "Enter an input file name: "; chomp (my $choice = <STDIN>); open (FH, '<', "C:/Users/tsukk/$choice"); while (my $data = <FH>) { push @array, $data; } my @largest = sort {$b<=>$a} @array; my @smallest = sort {$a<=>$b} @array; print "\nThe largest number is: "; print $largest[0], "\n"; print "The smallest number is: "; print $smallest[0]; print "The average of the numbers is: "; printf "%.1f\n", average(@array); print "\n"; $average = average(@array); 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"; } } print "\nDo it again? (Yes or No): "; chomp (my $yesno=<STDIN>); if($yesno ne 'Yes') { print "Goodbye.\n"; exit; } } sub average { if (@array) { my @temp = @_; my $sum = 0; foreach (@temp) { $sum = $sum + $_; } return $sum/@temp; } }

thank you!


In reply to 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.