Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: counting backward

by BrowserUk (Pope)
on Mar 02, 2013 at 14:46 UTC ( #1021420=note: print w/ replies, xml ) Need Help??


in reply to counting backward

Because the range operator is defined as low .. high and as 10 is higher than 0, it does nothing.

There are a couple of ways of doing what you want:

  • for my $i ( reverse 0 .. 10 ) { print $i }

    Which is okay for small ranges; but creates a huge list for large ones.

  • for my $i ( -10 .. 0 ) { print -$i }

    Which works for any size range without creating a huge list.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: counting backward
Select or Download Code
Re^2: counting backward (optimize foreach reverse low .. high
by Anonymous Monk on Mar 02, 2013 at 15:13 UTC

    Which is okay for small ranges; but creates a huge list for large ones.

    :) seems like an easy candidate for optimization , since this is optimized for (reverse @a) is optimized

    perl -le " @f = 1 .. 100_000; for(reverse @f ){ $f=$_; $f==$#f and sca +lar<> } " perl -le " @f = 1 .. 100_000; for( @f ){ $f=$_; $f==$#f and scalar<> } + "
      AFAIK for( @f ) is optimized to act like an iterator without expanding and caching the list.

      But in for(reverse @f ) there is no for-context which helps reverse to act differently.

      I'm to lazy for benchmarks (we had them before), but this was surely not fixed in 5.8.8

      Cheers Rolf

        You're wrong
        for(reverse @f) has been optimised not to expand the array since 5.10.0.

        Dave.

      But you still had to create a huge array in order to benefit from the optimisation; which kinda negates the purpose.

      This: perl -E"@a=1..1e9; for( reverse @a ) { print $_; <> }" consumes 18GB (that's Gigabytes!) of memory

      Whereas this: perl -E"for( -1e9..1 ) { print -$_; <> }" iterates the same range and consumes only 1.8MB of ram.

      Four orders of magnitude more memory, or a minus sign. My choice is simple; how about yours?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Four orders of magnitude more memory, or a minus sign. My choice is simple; how about yours?

        I'll use minus sign until perl5-porter optimizes foreach reverse rangeop

        Or i'll use for;;;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021420]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-12-29 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls