Learning Perl

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

Learning Perl

Post by Dwere134 on Fri Apr 08, 2011 10:59 am
([msg=56144]see Learning Perl[/msg])

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
Experienced User
 
Posts: 83
Joined: Mon Sep 27, 2010 5:06 pm
Blog: View Blog (0)


Re: Learning Perl

Post by mojo1948 on Fri Apr 08, 2011 12:03 pm
([msg=56146]see Re: Learning Perl[/msg])

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.
User avatar
mojo1948
Experienced User
Experienced User
 
Posts: 60
Joined: Sun Jul 18, 2010 5:45 am
Blog: View Blog (0)


Re: Learning Perl

Post by Dwere134 on Fri Apr 08, 2011 2:04 pm
([msg=56151]see Re: Learning Perl[/msg])

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
Experienced User
 
Posts: 83
Joined: Mon Sep 27, 2010 5:06 pm
Blog: View Blog (0)


Re: Learning Perl

Post by mShred on Fri Apr 08, 2011 9:15 pm
([msg=56157]see Re: Learning Perl[/msg])

You could just define it where you defined your other variables. Add "my @list;" and it should be golden. Tested it out as well.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1686
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Learning Perl

Post by Dwere134 on Mon Apr 11, 2011 11:17 am
([msg=56243]see Re: Learning Perl[/msg])

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
Experienced User
 
Posts: 83
Joined: Mon Sep 27, 2010 5:06 pm
Blog: View Blog (0)


Re: Learning Perl

Post by mShred on Mon Apr 11, 2011 6:03 pm
([msg=56256]see Re: Learning Perl[/msg])

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.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1686
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Learning Perl

Post by jgreen45 on Tue Apr 12, 2011 4:47 am
([msg=56272]see Re: Learning Perl[/msg])

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/
User avatar
jgreen45
Poster
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