AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Anyone here, who read the "Llama Book 4th edition"

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
AGU
Guest





PostPosted: Tue Apr 08, 2008 6:09 am    Post subject: Anyone here, who read the "Llama Book 4th edition" Reply with quote

I hope some of you AHK members is a perl professional. Smile

I have a question concerning the exercises to Chapter 4. Especially exercise 4-3.

Code:
#!/usr/bin/perl -w
use strict;

sub average {
         if (@_ == 0) { return }
         my $count = @_;
         my $sum = &total(@_);
         $sum/$count;
}
sub above_average {
         my $average = &average(@_);
         my @list;
         foreach my $element (@_) {
                        if ($element > $average) {
                                 push @list, $element;
                        }
         }
         @list;
}
sub total {
         my $sum;
         foreach (@_) {
                        $sum += $_;
         }
         $sum;
}

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



Why is the control variable named $element instead of using Perl's favorite default, $_?. I've tried and $_ is working too. So I simply don't get it.
Why are they using $element?

Code:
sub above_average {
         my $average = &average(@_);
         my @list;
         foreach (@_) {
                        if ($_ > $average) {
                                 push @list, $_;
                        }
         }
         @list;
}
As said, this is working, too.
____________________
Cheers AGU
Back to top
hoppfrosch



Joined: 25 Jan 2006
Posts: 16
Location: Somewhere in Germany

PostPosted: Tue Apr 08, 2008 7:26 am    Post subject: Reply with quote

I think there is no special/hidden meaning behind this - it's just "TIMTOWTDI" ("There is more than one way to do it).

I personally prefer the notation as used in the example, since it's more clearly for reading - but that's a personal preference.
Back to top
View user's profile Send private message
AGU
Guest





PostPosted: Tue Apr 08, 2008 3:31 pm    Post subject: Reply with quote

Thx for your answer hoppfrosch. Very Happy

Quote:
Why is the control variable named $element instead of using Perl's favorite default, $_?

Well the author is asking the question explicitly within the solution.

In exercise 4-1 they're creating the sub total (which is also used within 4-3) where they use perls default variable $_. So did I, which is why I used them within 4-3 as well.
Code:
sub total {
         my $sum;
         foreach (@_) {
                        $sum += $_;
         }
         $sum;
}

When checking the solution to 4-3, I was surprised they used another control variable. At that point I also thought about "TIMTOWTDI" but in the last paragraph they're asking the above mentioned "magic" question (within parantheses).
So I thought there must be something special behind this. Honestly I don't get it. I'm sitting here, racking my brains.
____________________
Cheers AGU
Back to top
Guest






PostPosted: Wed Apr 09, 2008 12:38 pm    Post subject: Reply with quote

AGU wrote:
I'm sitting here, racking my brains.

...wow, cool, is that the new Human 2.0 model? I was only built with 1 brain...darn...maybe I should buy the upgrade? How much is it?

(Sorry, I really had to do this...LOL...{oh dear, now that I think about it, I feel like BoBo, making a smart-ass reply that isn't helpful at all})...
Back to top
BoBoĻ
Guest





PostPosted: Wed Apr 09, 2008 12:47 pm    Post subject: Reply with quote

Quote:
now that I think about it, I feel like BoBo, making a smart-ass reply that isn't helpful at all
@ Guest (AKA Beavis) There's a reason why BoBo is named BoBo --> Narrenfreiheit. And btw, there's always (~98,99%) a meaning behind what you called "smart-ass reply that isn't helpful at all".
Well - you've to be a smart-ass to realize it. Why not have another try! WinkLaughing
Back to top
AGU
Guest





PostPosted: Thu Apr 10, 2008 4:27 pm    Post subject: Reply with quote

TBH I checked http://dict.leo.org to look for an appropiate translation for "sich den Kopf zerbrechen." The site says Arrow to rack one's brains

So I wrote "brains" because the site says so. Is it wrong then?
_______________
Cheers AGU
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group