in reply to [Homework Question] Subroutines & References

sub above_mean { $i == mean(@list_of_numbers); foreach (@list_of_numbers) { if ($a > $i) { print "$a is above mean, which is mean(@list_of_numbers)" +; } #........^ this is missing } #...^ so is this }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^2: [Homework Question] Subroutines & References
by Hayest (Acolyte) on Feb 11, 2015 at 16:28 UTC
    #!/usr/bin/perl use warnings; use List::Util qw(sum); my @list_of_numbers = (1 .. 50); my $i = (); my $a = (); sub mean { return sum(@_)/@_; } sub above_mean { $i == mean(@list_of_numbers); foreach (@list_of_numbers) { if ($a > $i) { print "$a is above mean, which is mean(@list_of_numbers)"; } } } print above_mean(@list_of_numbers);

    This now returns:

    Use of uninitialized value $a in numeric gt (>) at main.pl line 16.

    However, I thought I declared $a earlier in my program?

      == is the numeric comparison operator, not the assignment operator, which is quite similar: =. See perlop for details. warnings should have told you:
      Useless use of numeric eq (==) in void context at ...

      Also, you don't populate $a anywhere. Maybe, you meant

      foreach my $a (@list_of_numbers) {
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        I realized the == error once it executed, however, you helped out with the suggestion of the foreach statement. I've got it running properly now. Thank you!
      print above_mean(@list_of_numbers);

      Just as a matter of curiosity, you might try this variation on the quoted statement:
          print above_mean();
      Does calling the  above_mean() function without any parameters make any difference to the value returned by the function? If not, why not?


      Give a man a fish:  <%-(-(-(-<

        calling above_mean(); function without parameters make does not make any difference to the value returned by the function. I believe this is because @numbers (which I had as the parameter) is already declared inside of my subroutine above_mean - is this accurate?