Every other

Mark Fowler mark at twoshortplanks.com
Fri Oct 30 10:35:33 GMT 2009


On Fri, Oct 30, 2009 at 9:48 AM, Bob MacCallum <uncoolbob at gmail.com> wrote:
> I clearly have more urgent things to do...
>
> @new = @old[map $_*2, 0..$#old/2];

Compact, but not overly readable and op-wise not ultra efficient:

perl -MO=Terse -e ' @new = @old[map $_*2, 0..$#old/2];'
LISTOP (0x100208ca0) leave [1]
    OP (0x100202620) enter
    COP (0x100208c40) nextstate
    BINOP (0x100208c00) aassign [13]
        UNOP (0x100209de0) null [142]
            OP (0x100208bd0) pushmark
            LISTOP (0x100209d00) aslice
                OP (0x100209d40) pushmark
                LOGOP (0x100209cc0) mapwhile [12]
                    LISTOP (0x100209c20) mapstart
                        OP (0x100209c60) pushmark
                        UNOP (0x100209c90) null
                            BINOP (0x1002091a0) multiply [6]
                                UNOP (0x100209140) null [15]
                                    PADOP (0x100209110) gvsv  GV
(0x100804e28) *_
                                SVOP (0x100209170) const [15] IV
(0x100813058) 2
                        UNOP (0x100209bf0) null
                            UNOP (0x100206d30) flop
                                UNOP (0x100206d00) flip [11]
                                    LOGOP (0x100206cc0) range [10]
                                        SVOP (0x1002e3240) const [16]
IV (0x100804fd8) 0
                                        BINOP (0x100206c80) divide [9]
                                            UNOP (0x100206c20) av2arylen
                                                UNOP (0x100206bf0) rv2av [8]
                                                    PADOP
(0x100206bc0) gv  GV (0x100812fe0) *old
                                            SVOP (0x100206c50) const
[14] IV (0x100813040) 2
                UNOP (0x100220830) rv2av [4]
                    PADOP (0x10020a6f0) gv  GV (0x100812fe0) *old
        UNOP (0x100209d70) null [142]
            OP (0x100209db0) pushmark
            UNOP (0x100208eb0) rv2av [2]
                PADOP (0x100201c30) gv  GV (0x100812f98) *new
-e syntax OK

Compared to:

perl -MO=Terse -e 'my $i; @new = grep { $i != $i } @old;'
LISTOP (0x100208f60) leave [1]
    OP (0x100202620) enter
    COP (0x100208fd0) nextstate
    OP (0x100201c30) padsv [1]
    COP (0x100209d50) nextstate
    BINOP (0x100209d10) aassign [7]
        UNOP (0x100209ca0) null [142]
            OP (0x100209ce0) pushmark
            LOGOP (0x100206d10) grepwhile [6]
                LISTOP (0x100206ca0) grepstart
                    OP (0x100206ce0) pushmark
                    UNOP (0x100206d50) null
                        UNOP (0x100206c70) null
                            LISTOP (0x100206b50) scope
                                OP (0x100209150) null [177]
                                BINOP (0x100209110) ne
                                    OP (0x100220830) padsv [1]
                                    OP (0x1002e3240) padsv [1]
                    UNOP (0x100206c10) rv2av [5]
                        PADOP (0x1002091b0) gv  GV (0x100813058) *old
        UNOP (0x100209c60) null [142]
            OP (0x100206c40) pushmark
            UNOP (0x10020a6f0) rv2av [3]
                PADOP (0x100208eb0) gv  GV (0x100812fc8) *new
-e syntax OK

Either way, it's a lot of ops to pull things out of an array.


More information about the london.pm mailing list