#!/usr/bin/perl use strict; use warnings; use 5.016; # if 5.016 is available, allows "say;" otherwise # delete this and replace the "say"s with "print"+ newlines # if 5.016 or > avail, uncomment the 'say "\t DEBUG..."' lines # to see values they present. # 1124160 my ( $input, $num, @nums, $num_total ); START: say "Type a number and or type 'q' when finished:"; chomp ($input = ); if ( ($input eq 'Q') || ($input eq 'q') ) { goto END_INPUT; } elsif ($input =~ /\d+/){ push @nums, $input; goto START; } else { print "Input invalid; type a number or 'q': \n"; } END_INPUT: $num_total = total(@nums); say "\t DEBUG: \$num_total: $num_total"; my $quant_entries = scalar(@nums); # say "\t DEBUG\$quant_entries: $quant_entries"; my $average = ($num_total / $quant_entries); # say "\t DEBUG \$average: $average"; print "The total of the numbers in your list is $num_total.\n"; print "The average of the numbers is $average .\n"; my @list = above_average(\@nums, \$average); # see PASS BY REFERENCE; otherwise array # FLATTENs to multiple values before # it's (use-able or used) in the sub print "The following numbers are above the group average: "; for $_ ( @list ) { print "$_, "; } ####### subs sub total { my $sum; foreach (@_) { # @_ contains the 2 arguments you passed from at Ln 31 $sum += $_; # compare to above_average(), which uses a different method # of passing in data } return $sum; } #### end sub total() ##### sub above_average { my ( @avg, @nums, $num ); # These are NEW vars - DISTINCT from those in the mainline my ($avg, @list ); # These are ALSO new and also localized to this sub # shift takes one arg (var) off @_ which is the ( default) # container for values passed to a sub my @localNums = @{$_[0]}; # Deref first arg (a ref) from Ln 38 # say "\t DEBUG at Ln 59: \@localNums: @localNums"; $avg = ${$_[1]}; # Deref the second var passed from Ln 38 # say "\t DEBUG at Ln 61: \$avg: $avg"; for (@localNums) { if ( $_ > $avg ) { push \@list, $_; } } return @list; } #######end sub above_average() #########