Common Perl Pitfalls
Peter J. Holzer
hjp at wsr.ac.at
Mon Jan 24 03:49:44 CST 2000
On 2000-01-23 16:19:21 +0100, Roland Bauer wrote:
> Dort wird zB darauf hingewiesen, dass das folgende eher nicht
> Perl-typisch ist:
>
> $size = @array;
> for ($i = 0; $i < $size; $i++){
> $element = $array[$i]; # or even worse, @array[$i]
> # et cetera
> }
>
> Besser:
>
> for (@array){ ... } # or
"Besser" hängt davon ab, was die Schleife tun soll. Manchmal braucht man
den Index, und in dem Fall halte ich
for ($i = 0; $i < scalar(@array); $i++) {
do_something_with($i, $array[$i]);
}
für leserlicher als
$i = 0;
for $element (@array) {
do_something_with($i, $element);
$i++
}
Außerdem hat $i dann den richtigen Scope, wenn man my verwendet.
hp
PS: Wie man merkt, vermeide ich $_ wo immer möglich.
--
_ | Peter J. Holzer | Nobody should ever have to be
|_|_) | Sysadmin WSR / Obmann LUGA | ashamed if they have a secret love
| | | hjp at wsr.ac.at | for writing computer programs that
__/ | http://wsrx.wsr.ac.at/~hjp/ | actually work. -- Donald E. Knuth
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 371 bytes
Desc: not available
Url : http://mail.pm.org/archives/vienna-pm/attachments/20000124/32f1ffd9/attachment.bin
More information about the Vienna-pm
mailing list