Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: shift on empty array in list context broken

by tybalt89 (Monsignor)
on Jul 13, 2019 at 21:05 UTC ( [id://11102801]=note: print w/replies, xml ) Need Help??


in reply to shift on empty array in list context broken

It doesn't look like shift has a list context, at least according to perldoc. That makes some sense, since it can only return one thing, unlike splice, which can return zero to many.

I'd use

while( @x ) { print shift @x, ',' }

for your last example.

Replies are listed 'Best First'.
Re^2: shift on empty array in list context broken
by LanX (Saint) on Jul 13, 2019 at 21:22 UTC
    > It doesn't look like shift has a list context, at least according to perldoc.

    well splice also says

    The following equivalences hold
    ... shift(@a) splice(@a,0,1)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      splice lied! It's almost sort-of nearly true, but not completely true :)

        splice didnt lie

        The array is manipulated exactly like shift would manipulate it.

        The return value is exactly as documented for splice/shift.

        The equivalences chart is part but not the whole documentation.

        > splice lied!

        I prefer orthogonal design :/

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      On the other hand, try

      my @x = (); my $x = splice @x, 0, 1;

      What splice says is true in scalar context.

        what do you mean?

        DB<17> ($x= splice @x, 0, 1 ) and print "true" DB<18> p !! splice @x, 0, 1 DB<19>

        it's false for me.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      That should be fixed. Feel free to use perlbug to submit a bug report. Please include a suggested fix.

        Reported without a precise suggestion to fix.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      >well splice also says
      > The following equivalences hold
      > ...
      > shift(@a)     splice(@a,0,1)

      It would appear shift is doing something like this:

      DB<232> x $x=4; while( ($y)= ( $u = splice( @t,0,1 ))){ last unless +$x--; print ":$_: " } :: :: :: :: empty array

      which would make the equivalence:

      shift(@a) === ( $t = splice( @a,0,1 ) )

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found