Llama book exercise

Discussion in 'Perl Misc' started by PerseP, Mar 12, 2007.

  1. PerseP

    PerseP Guest

    Hi,
    I'm learning Perl by reading the "llama book". It happens that on the
    answer to exercise 3 from chapter 4 (page 234), the authors ask why
    the control variable in the subroutine &above_average is named
    $element instead of using Perl's default $_.
    The code is:

    #!/usr/bin/perl

    use strict;

    sub total {
    my $sum;
    foreach(@_) {
    $sum+=$_;
    }
    $sum;
    }

    sub average {
    if (@_ ==0) { return; }
    my $count =@_;
    my $sum=&total(@_);
    $sum/$count;
    }

    sub above_average {
    my $average=&average(@_);
    my @list;
    my $element;
    foreach $element (@_) {
    if($element > $average) {
    push @list,$element;
    }
    }
    @list;
    }

    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";

    I've run the code as it's given above and with my initial answer,using
    $_ instead of $elements:
    sub above_average {
    my $average=&average(@_);
    my @list;
    my $element;
    foreach (@_) {
    if($_ > $average) {
    push @list,$_;
    }
    }
    @list;
    }

    Both give the same output. So is there any point in using $element
    instead of $_ as the control variable?

    Thanks
    PerseP, Mar 12, 2007
    #1
    1. Advertising

  2. PerseP

    brian d foy Guest

    In article <>,
    PerseP <> wrote:

    > I'm learning Perl by reading the "llama book". It happens that on the
    > answer to exercise 3 from chapter 4 (page 234), the authors ask why
    > the control variable in the subroutine &above_average is named
    > $element instead of using Perl's default $_.



    > Both give the same output. So is there any point in using $element
    > instead of $_ as the control variable?


    $_ is a global variable. Instead of getting into the bad habit of using
    it when you don't need it, we suggest that you use your own variable
    for the foreach. It might seem a bit silly for the simple exercise, but
    it's never too early to start good coding practices. :)

    --
    Posted via a free Usenet account from http://www.teranews.com
    brian d foy, Mar 12, 2007
    #2
    1. Advertising

  3. PerseP

    -berlin.de Guest

    PerseP <> wrote in comp.lang.perl.misc:
    > Hi,
    > I'm learning Perl by reading the "llama book". It happens that on the
    > answer to exercise 3 from chapter 4 (page 234), the authors ask why
    > the control variable in the subroutine &above_average is named
    > $element instead of using Perl's default $_.
    > The code is:


    [...]

    > sub above_average {
    > my $average=&average(@_);
    > my @list;
    > my $element;
    > foreach $element (@_) {
    > if($element > $average) {
    > push @list,$element;
    > }
    > }
    > @list;
    > }


    [...]

    > I've run the code as it's given above and with my initial answer,using
    > $_ instead of $elements:
    > sub above_average {
    > my $average=&average(@_);
    > my @list;
    > my $element;
    > foreach (@_) {
    > if($_ > $average) {
    > push @list,$_;
    > }
    > }
    > @list;
    > }
    >
    > Both give the same output. So is there any point in using $element
    > instead of $_ as the control variable?


    It must be a stylistic point the authors are making there. As you
    have observed, technically it is quite possible to use $_ instead.

    Anno
    -berlin.de, Mar 12, 2007
    #3
  4. PerseP

    PerseP Guest

    On Mar 12, 10:11 pm, brian d foy <> wrote:

    > $_ is a global variable. Instead of getting into the bad habit of using
    > it when you don't need it, we suggest that you use your own variable
    > for the foreach. It might seem a bit silly for the simple exercise, but
    > it's never too early to start good coding practices. :)


    Hi,

    thanks for answering.The answer is a bit contradictory as there are
    other examples in the book that use the $_ instead of a private
    variable thus not being examples of good coding but as there are only
    short examples there's nothing wrong with that.

    BTW, may I notice there is a typo on that same page of the book
    "Learning Perl 4th edition 0-596-10105-8 " in the same exercise where
    the declaration of my $element is missing so it gives a compile error.
    I've already reported it to the editor.
    PerseP, Mar 13, 2007
    #4
  5. PerseP

    PerseP Guest

    > $_ is a global variable. Instead of getting into the bad habit of using
    > it when you don't need it, we suggest that you use your own variable
    > for the foreach. It might seem a bit silly for the simple exercise, but
    > it's never too early to start good coding practices. :)



    Hi,
    thanks for the reply. I think it's a bit contradictory because the are
    other examples in the book that use $_ as a control variable instead
    of defining a new private one. As they are only examples for a book I
    guess it's not that bad.

    BTW there is a typo mistake in the same exercise on page 234 "Learning
    Perl 4th edition 0-596-10105-8" where the declaration of the variable
    "my $element" is missing thus giving a compilation error. I've already
    reported it to the editor.
    PerseP, Mar 13, 2007
    #5
  6. PerseP

    brian d foy Guest

    In article <>,
    PerseP <> wrote:

    > On Mar 12, 10:11 pm, brian d foy <> wrote:
    >
    > > $_ is a global variable. Instead of getting into the bad habit of using
    > > it when you don't need it, we suggest that you use your own variable
    > > for the foreach. It might seem a bit silly for the simple exercise, but
    > > it's never too early to start good coding practices. :)


    > thanks for answering.The answer is a bit contradictory as there are
    > other examples in the book that use the $_ instead of a private
    > variable thus not being examples of good coding but as there are only
    > short examples there's nothing wrong with that.



    It's not contradictory, it's just that we made an additional point in
    that particular exercise. For most of the book we ignore most of the
    things that we *could* talk about to focus on the thing that we want
    you to learn. :)

    --
    Posted via a free Usenet account from http://www.teranews.com
    brian d foy, Mar 14, 2007
    #6
  7. PerseP

    PerseP Guest


    > It's not contradictory, it's just that we made an additional point in
    > that particular exercise. For most of the book we ignore most of the
    > things that we *could* talk about to focus on the thing that we want
    > you to learn. :)
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    Ok, I understand. Thanks
    PerseP, Mar 14, 2007
    #7
  8. In article <>,
    "PerseP" <> wrote:

    > I think it's a bit contradictory...


    There's More Than One Way To Do It.

    Of course, we all tend to react to that much freedom by saying, from
    time to time:

    Do It This Way Only.

    Sometimes, we're right.
    --
    Xiong Changnian
    xiong102ATxuefangDOTcom

    --
    Posted via a free Usenet account from http://www.teranews.com
    Xiong Changnian, Apr 1, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    430
    John Saunders
    Aug 6, 2003
  2. Thanasis \(sch\)

    awt book+java animation book

    Thanasis \(sch\), Oct 13, 2004, in forum: Java
    Replies:
    1
    Views:
    589
    Alex Hunsley
    Oct 14, 2004
  3. coltrane
    Replies:
    2
    Views:
    468
    coltrane
    Sep 27, 2006
  4. Gio

    Puzzle Book & Answer Book

    Gio, Apr 17, 2008, in forum: C Programming
    Replies:
    1
    Views:
    388
    santosh
    Apr 17, 2008
  5. FA: Llama book (UK)

    , Sep 27, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    112
    John W. Krahn
    Sep 28, 2006
Loading...

Share This Page