in reply to (Ovid) Re: Newbie realisation about
in thread Newbie realisation about

Whereas in idiomatic Perl, you'll typicall see things like the following:
for ( 0 .. 19 ) { print "$_\n"; }

... with the reminder that ( 0 .. 1000000 ) would put an undue strain on your machine's memory as it generates all million (plus one) numbers in a list. And if you have two nested loops, you have to do the 'hot potatoe' thing with $_ so that its value doesn't get overwritten.

for ( 0..7 ) { my $Col = $_; # Eek! Save $_ otherwise this value is lost for ( 0..7 ) { # Do something in an 8x8 matrix. if ( $Matrix[ $Col ][ $_ ] == 4 ) ... } }
It's a matter of preference .. I would rather just write
for ( my $Col = 0; $Col < 8; $Col++ ) { for ( my $Row = 0; $Row < 8; $Row++ ) { # Do something in an 8x8 matrix. if ( $Matrix[ $Col ][ $Row ] == 4 ) ... } }
But that's my years of C programming colouring my view of Perl.

--t. alex

"Of course, you realize that this means war." -- Bugs Bunny.

Replies are listed 'Best First'.
(Ovid) Re(3): Newbie realisation about
by Ovid (Cardinal) on Feb 02, 2002 at 08:16 UTC

    talexb wrote:

    ... with the reminder that ( 0 .. 1000000 ) would put an undue strain on your machine's memory as it generates all million (plus one) numbers in a list.

    That's no longer true for recent versions of Perl. From perldoc perlop (perl -v 5.6.1):

    The range operator is useful for writing "foreach (1..10)" loops and for doing slice operations on arrays. In the current implementation, no temporary array is created when the range operator is used as the expression in "foreach" loops, but older versions of Perl might burn a lot of memory when you write something like this:
    for (1 .. 1_000_000) { # code }

    talexb also wrote:

    And if you have two nested loops, you have to do the 'hot potatoe' thing with $_ so that its value doesn't get overwritten.

    Also not accurate. In that case, simply name the resulting value!

    for my $first ( 0..7 ) { for my $second ( 0..7 ) { # Do something in an 8x8 matrix. if ( $Matrix[ $first ][ $second ] == 4 ) ... } }

    Idiomatic Perl to the rescue again :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re (tilly) 3: Newbie realisation about
by tilly (Archbishop) on Feb 03, 2002 at 03:43 UTC
    In addition to the points made by Ovid, Perl-style foreach loops are a lot less error prone than C-style for loops are.

    People who use C-style loops, whether it be in C or another language, typically have off-by-one errors as their most common type of bug. By simply switching to Perl-style loops when programming in Perl, you kill the opportunity for this mistake, and therefore will have fewer bugs to track down.

    I have seen this happen in enough cases that I can't accept the argument that it is simply personal preference. When you have a choice, use the Perlish looping mechanism. The time saved in avoiding debugging will quickly make this pay for itself.