in reply to TIMTOWTDI - printing an array

map {print} @arr;

Replies are listed 'Best First'.
Re^2: TIMTOWTDI - printing an array
by tobyink (Canon) on Jun 20, 2012 at 18:03 UTC

    For that matter,

    grep { print } @array;

    Or even...

    use List::MoreUtils qw<first>; first { not print } @array;

    I can imagine many newbies scratching their heads if they saw that one! But I told it to "not print" the array! ;-)

    Update: of course, there's the venerable...

    print shift @array while @array;

    ... which prints, but also dismantles an array.

    my @r;@r = reverse @array and do { print pop @r while @r }; # Now I'm just being silly!
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      print shift @array while @array;

      ... which prints, but also dismantles an array.

      Using that idiom inside an anonymous subroutine is non-destructive!

      knoppix@Microknoppix:~$ perl -E ' > @arr = qw{ 11 22 33 44 }; > sub { say shift while @_ }->( @arr ); > say qq{@arr};' 11 22 33 44 11 22 33 44 knoppix@Microknoppix:~$

      Cheers,

      JohnGG