Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Pure variable confusion!

by ww (Archbishop)
on Apr 22, 2015 at 16:12 UTC ( [id://1124284]=note: print w/replies, xml ) Need Help??


in reply to Pure variable confusion!

See also answers at passing paramters to a sub, esp GrandFather's and those others dealing with PASS BY REFERENCE as opposed to PASS BY VALUE. And not just by the way, you would have found many nodes offering answers by using Super Search.

And here's a tutorial-like example of not only "using information generated outside a subroutine inside the subroutine and vice versa" but also of some other aspects of an approach to other matters.

#!/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 <ENTER> or type 'q' when finished:"; chomp ($input = <STDIN>); 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 REFERENC +E; otherwise array # FLATTENs to multiple + values before # it's (use-able or us +ed) 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 t +hose in the mainline my ($avg, @list ); # These are ALSO new and also localize +d to this sub # shift takes one arg (var) off @_ whi +ch 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() #########

Much of this is perhaps redundant at this late date and, in any case, note that (TIMTOWTDI) your preferences may not match my style.


Come, let us reason together: Spirit of the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1124284]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found