in reply to Array confusion.

A non-destructive way map'ing pop inside an anonymous subroutine.

johngg@shiraz:~/perl > perl -Mstrict -Mwarnings -E ' my @arr = ( q{a} .. q{z} ); my @rev = sub { map { pop } 1 .. @_ }->( @arr ); say qq{@arr}; say qq{@rev};' a b c d e f g h i j k l m n o p q r s t u v w x y z z y x w v u t s r q p o n m l k j i h g f e d c b a

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Array confusion.
by BrowserUk (Patriarch) on Feb 14, 2017 at 23:51 UTC

    A slightly simplified version:

    @a = 'a'..'z';; @b = sub{ map pop, @_ }->( @a );; print @b;; z y x w v u t s r q p o n m l k j i h g f e d c b a

    Or even just:

    @c = map pop @a, @a;; print @c;; z y x w v u t s r q p o n m l k j i h g f e d c b a

    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". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.
      @c = map pop @a, @a;;

      Much simpler, true, but destructive if that is a concern.

      johngg@shiraz:~/perl > perl -Mstrict -Mwarnings -E ' my @arr = ( q{a} .. q{z} ); my @rev = map pop @arr, @arr; say qq{@arr}; say qq{@rev};' z y x w v u t s r q p o n m l k j i h g f e d c b a

      Cheers,

      JohnGG

        but destructive if that is a concern.

        True. Then:

        @a = 'a'..'z';; unshift @d, $_ for @a;; print @d;; z y x w v u t s r q p o n m l k j i h g f e d c b a print @a;; a b c d e f g h i j k l m n o p q r s t u v w x y z

        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". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice.