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

In reply to Re: Pure variable confusion! by ww
in thread Pure variable confusion! by jmvbxx

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.