 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
AGU Guest
|
Posted: Tue Apr 08, 2008 6:09 am Post subject: Anyone here, who read the "Llama Book 4th edition" |
|
|
I hope some of you AHK members is a perl professional.
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
|
Posted: Tue Apr 08, 2008 7:26 am Post subject: |
|
|
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 |
|
 |
AGU Guest
|
Posted: Tue Apr 08, 2008 3:31 pm Post subject: |
|
|
Thx for your answer hoppfrosch.
| 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
|
Posted: Wed Apr 09, 2008 12:38 pm Post subject: |
|
|
| 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
|
Posted: Wed Apr 09, 2008 12:47 pm Post subject: |
|
|
| 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!   |
|
| Back to top |
|
 |
AGU Guest
|
Posted: Thu Apr 10, 2008 4:27 pm Post subject: |
|
|
TBH I checked http://dict.leo.org to look for an appropiate translation for "sich den Kopf zerbrechen." The site says to rack one's brains
So I wrote "brains" because the site says so. Is it wrong then?
_______________
Cheers AGU |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|