## Learning Perl

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

### Learning Perl

Alright so I'm teaching myself Perl, out of O'Reilly's Learning Perl by Randal L. Schwartz, Tom Phoenix, and Brian D Foy.
I've made it to Chapter four, thus far (Subroutines)
I'm on an exercise that I can kind of half-way get, but I'm not sure how to complete the rest of it.

The Exercise is as follows:
Extra credit exercise: Write a subroutine, called above_average, that takes a list of numbers and returns the ones that are above average(mean). (Hint: Make another subroutine that calculates the average by dividing the total by the number of items.) Try your subroutine(s) in this test program:
Code: Select all
`my @fred = above_average(1..10);print "\@fred is @fred\n";print "(Should be 6, 7, 8, 9, 10)\n"my @barney = above_average(100, 1..10);print "\@barney is @barney\n";print "(Should be just 100)\n";`

My code is as follows:
Code: Select all
`my @fred = above_average(1..10);print "\@fred is @fred\n";print "(Should be 6 7 8 9 10)\n";my @barney = above_average(100, 1..10);print "\@barney is @barney\n";print "(Should be just 100)\n";sub above_average {   my \$sum;   my \$average;   my \$count = @_;   foreach (@_) {      \$sum += \$_;      \$average = \$sum/\$count      }   foreach my \$element (@_) {      if (\$element > \$average) {      push @list, \$element;      }   }   @list;}`

But it returns 6,7,8,9,10 for both @fred and @barney, where @barney should only return 100. How do I edit my code to get it to return only 100?

Thanks so much for your constructive help.
Dwere (David)
Goatboy wrote:
Dwere wrote:I'm not one to start some branch of religion though. Not my thing.

Of course if you wanted to, you could call it the Davidians!
Dwere134
Experienced User

Posts: 83
Joined: Mon Sep 27, 2010 5:06 pm
Blog: View Blog (0)

### Re: Learning Perl

You need to define @list as my @list in your above_average routine. All variables in Perl are global variables unless defined locally. It is usually referred to as scope.
Never stop learning.

mojo1948
Experienced User

Posts: 60
Joined: Sun Jul 18, 2010 5:45 am
Blog: View Blog (0)

### Re: Learning Perl

Thanks. So would I just declare it as push my @list, \$element; instead? or do i have to declare it before the foreach loop?
Dwere (David)
Goatboy wrote:
Dwere wrote:I'm not one to start some branch of religion though. Not my thing.

Of course if you wanted to, you could call it the Davidians!
Dwere134
Experienced User

Posts: 83
Joined: Mon Sep 27, 2010 5:06 pm
Blog: View Blog (0)

### Re: Learning Perl

You could just define it where you defined your other variables. Add "my @list;" and it should be golden. Tested it out as well.
For those about to hack, I salute you.
teehee

mShred
Administrator

Posts: 1899
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)

### Re: Learning Perl

Thanks, I appreciate the help. But since I'm trying to grasp at understanding and not just completing...
Could you tell me WHY that worked and why what I did before was wrong or incomplete?
Dwere (David)
Goatboy wrote:
Dwere wrote:I'm not one to start some branch of religion though. Not my thing.

Of course if you wanted to, you could call it the Davidians!
Dwere134
Experienced User

Posts: 83
Joined: Mon Sep 27, 2010 5:06 pm
Blog: View Blog (0)

### Re: Learning Perl

Dwere134 wrote:Thanks, I appreciate the help. But since I'm trying to grasp at understanding and not just completing...
Could you tell me WHY that worked and why what I did before was wrong or incomplete?

Well your array was a global array. Which means after the first subroutine, @list became 6-10. After the second subroutine, @list still had the 6-10 and it got the 100. But if you declare it as my @list, then it will be overwritten everytime the subroutine is executed essentially.
For those about to hack, I salute you.
teehee

mShred
Administrator

Posts: 1899
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)

### Re: Learning Perl

I think a link might help, i found this

I hope this helps
I can't come to bed...
Someone is WRONG on the internet

http://xkcd.com/386/

jgreen45
Poster

Posts: 106
Joined: Wed Feb 25, 2009 6:18 pm
Blog: View Blog (0)

Return to Interpreted Languages

### Who is online

Users browsing this forum: No registered users and 0 guests