# perlish way for ( 0 .. \$n) , but downcounting

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hi,

I know I can live without it or with a self written iterator
or . . . , but would still like to know
other perlish ways

creating an array with numbers from 0 to 9 i would use
@a = (0...9);
for the reverse
@a = reverse(0..9);

cycling trough 0 to n (n verly large)
I'd do
\$n=100000;
foreach \$v (0..\$n){ dowith(\$v) }
# If I understood well the (0..\$n) would NOT create a list, but act as
iterator

for the reverse I'd hesitate to use reverse() as it creates probably
the entire list first
so I'd do either
foreach \$v_up (0..\$n) { \$v_down = \$n-\$v; dowith(\$v); }

or foreach( \$v=\$n;\$v>=0;\$v--){ dowith(\$v); }

Does anybody have a 'nicer' way ?

## Re: perlish way for ( 0 .. \$n) , but downcounting

[snip]

Not pretty, but works without warnings:

my \$i = 1000

for ( -\$i .. 0 ) {
print "\$i\n";
\$i--;
}

Justin.

--
Justin C, by the sea.

## Re: perlish way for ( 0 .. \$n) , but downcounting

Justin C wrote:
) Not pretty, but works without warnings:
)
) my \$i = 1000
)
) for ( -\$i .. 0 ) {
)     print "\$i\n";
)     \$i--;
) }

Why not go all the way then:

for (-1000 .. 0) { dowith(-\$_) }

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Willem wrote:

Nicely done!

BugBear

## Re: perlish way for ( 0 .. \$n) , but downcounting

In article

Not any nicer, but different:

my \$v = \$n;
while( \$v-- >= 0 ) {
dowith(\$v);
}

--
Jim Gibson

## Re: perlish way for ( 0 .. \$n) , but downcounting

*SKIP*

Don't guess, benchmark first!

/proc/[number]/statm
Provides  information about memory status in pages.  The columns are:
size    total program size
resident resident set size
share    shared pages
text    text (code)
lib    library
data    data/stack
dt    dirty pages (unused in Linux 2.6)

21:48:38 65 [2:0]\$ perl -wle '
@x = (0 .. 10000); open \$h, "</proc/\$\$/statm"; print <\$h>'
Name "main::x" used only once: possible typo at -e line 1.
1000 498 275 256 0 307 0

21:49:22 66 [2:0]\$ perl -wle '
@x = reverse (0 .. 10000); open \$h, "</proc/\$\$/statm"; print <\$h>'
Name "main::x" used only once: possible typo at -e line 1.
1000 500 277 256 0 307 0

21:49:28 67 [2:0]\$ perl -wle '
@x = (0 .. 10000); @x = @x; open \$h, "</proc/\$\$/statm"; print <\$h>'
1001 497 275 256 0 308 0

21:49:35 68 [2:0]\$ perl -wle '
@x = (0 .. 10000); @x = reverse @x; open \$h, "</proc/\$\$/statm"; print <\$h>'
1000 498 277 256 0 307 0

21:49:41 69 [2:0]\$ perl -wle '
@x = (0 .. 10000); \$_++ foreach @x; open \$h, "</proc/\$\$/statm"; print <\$h>'
1000 501 279 256 0 307 0

21:50:14 70 [2:0]\$ perl -wle '
@x = (0 .. 10000); \$_++ foreach reverse @x;
open \$h, "</proc/\$\$/statm"; print <\$h>'
1001 503 280 256 0 308 0

21:50:22 71 [2:0]\$ perl -wle '
@x = (0 .. 10000); \$_++ while \$_ = shift @x;
open \$h, "</proc/\$\$/statm"; print <\$h>'
1000 501 279 256 0 307 0

If you think that the last does what it's intended to do, then you (me?)
are wrong. No fscking way! It's B<foreach>!

22:06:25 80 [2:0]\$ perl -wle '
@x = (0 .. 10000); while(1) { shift @x; @x or last };
open \$h, "</proc/\$\$/statm"; print <\$h>'
1001 505 283 256 0 308 0

Though that one does.  As you can see neither memory is released back
(just checked, B<undef> doesn't release too), no any significant
increase.  Just for clarity:

22:08:14 83 [2:0]\$ perl -wle '
@x = (0 .. 1); open \$h, "</proc/\$\$/statm"; print <\$h>'
Name "main::x" used only once: possible typo at -e line 1.
831 338 274 256 0 138 0

courious reader (BTW, how to say that abreviated?)

*CUT*

--
Torvalds' goal for Linux is very simple: World Domination

## Re: perlish way for ( 0 .. \$n) , but downcounting

Eric Pozharski wrote:

print 0+@x;

-mjc

LEXTOR ? :-)

## Re: perlish way for ( 0 .. \$n) , but downcounting

But benchmark something that makes sense.  None of the benchmarks
you posted make any sense in this context.

You need to use an example large enough to actually make a noticable dent
in memory, and you need to use the construct in question, which was
the range operators used directly in a foreach, with or without a reverse,
not a array being created first and then used in a foreach.

perl -le 'foreach (1..1e6) {}; print +(`ps -p \$\$ -o rss `)[1];'
1340

perl -le 'foreach (reverse 1..1e6) {}; print +(`ps -p \$\$ -o rss `)[1];'
72072

Yes, the reverse does build the whole list first.

Xho

--
The costs of publication of this article were defrayed in part by the
this fact.

## Re: perlish way for ( 0 .. \$n) , but downcounting

xhoster@gmail.com wrote:
*SKIP*

Yes, I've seen the light.  My fault.  I was misguided by

I should slightly disagree with that.  The impact was noticable enough
comparing to void (it was at the bottom).

However, I've made some experiments yet.  And found that if a list would
create reversed copy.

*SKIP*

I've got 11K pages (44Kb) for that.  64bit?  Then, I think, I've lost
that ball.  Pity on me.

*CUT*

--
Torvalds' goal for Linux is very simple: World Domination

## Re: perlish way for ( 0 .. \$n) , but downcounting

That is correct. Examining the code (pp_enteriter in pp_ctl.c) shows
that

1. if the argument to for() is just a range, it iterates without
creating the whole range;

2. if the argument is reverse(LIST), it expands LIST and then
iterates over it backwards;

3. otherwise, it expands the whole list.

Smells like an opportunity for a minor optimization patch... :)

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp.                    [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live.                   ben@morrow.me.uk