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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |