Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: split string using optimized code or perl one liner

by johngg (Canon)
on Feb 23, 2017 at 23:24 UTC ( [id://1182676]=note: print w/replies, xml ) Need Help??


in reply to Re^2: split string using optimized code or perl one liner
in thread split string using optimized code or perl one liner

On that basis the following should work but it doesn't ...

$ echo www.perlmonks.org.split.reduce.code.check | perl -lF'(\.)' -E's +ay sub{map pop,@_}->(@F)' $

... and I can't think why at the moment. Perhaps I'm misunderstanding what you have done :-/

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^4: split string using optimized code or perl one liner
by davido (Cardinal) on Feb 24, 2017 at 06:40 UTC

    People are likely to get mixed results here. I assume it's because of this line in perlsyn: "If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that."

    Why is this applicable?

    Let's simplify your construct a bit. my @O = sub {map pop, @_}->(@F) is very similar to my @O = map pop @F, @F, and that is quite similar to:

    my @O foreach (@F) { push @O, pop @F; }

    And the same rule applies, if you fiddle with the number of elements in the array Perl may get confused, or in other words "undefined behavior". The issue is that in both cases (map, or foreach), the list's size is evaluated up front before entering the loop, and that count probably remains fixed even if the size of the container you are iterating over shrinks. Then as you iterate, $_ is aliased to an element in the array. If the array shrinks, what does $_ alias to?

    Your code never looks at $_, instead just relying on the fact that before entering the loop Perl decides how many times to iterate. The fact that it works for some Perl versions is somewhat good luck, and luck is not reliable.


    Dave

      Normally I would do map { pop } 1 .. @_ as in my un-golfed original to make it clear that I am iterating a set number of times, since @_ is taken in scalar context in the range operator. I used the shorter version for golf having seen BrowserUk use it here. Your point about fiddling with elements is pertinent, and the behaviour shouldn't be relied upon, but is probably not the cause of the problem in this case.

      I am running 5.18.2 on Linux Mint 17 and adding the -an flags as mentioned by choroba and haukex (++ to them) gives the results expected:-

      $ echo www.perlmonks.org.split.reduce.code.check | perl -lanF'(\.)' -E +'say sub{map pop,@_}->(@F)' check.code.reduce.split.org.perlmonks.www $

      Note to self: start running multiple interpreter versions rather than being lazy and relying on whatever came with the Linux distro!

      Cheers,

      JohnGG

Re^4: split string using optimized code or perl one liner
by haukex (Archbishop) on Feb 24, 2017 at 05:59 UTC

    It works for me (Linux):

    $ echo www.perlmonks.org.split.reduce.code.check | \ perl -lF'(\.)' -E'say sub{map pop,@_}->(@F)' check.code.reduce.split.org.perlmonks.www
      It doesn't for me. I needed to add -na to the switches.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        It doesn't for me. I needed to add -na to the switches.

        Oh, right - perl5200delta:

        -F now implies -a and -a implies -n

        Previously -F without -a was a no-op, and -a without -n or -p was a no-op, with this change, if you supply -F then both -a and -n are implied and if you supply -a then -n is implied. You can still use -p for its extra behaviour. [perl #116190]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found