in reply to Re: (Ovid) Re: Newbie realisation about
in thread Newbie realisation about
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.
|
|---|