in reply to Re: TIMTOWTDI - printing an array
in thread TIMTOWTDI - printing an array

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'

Replies are listed 'Best First'.
Re^3: TIMTOWTDI - printing an array
by johngg (Canon) on Jun 20, 2012 at 23:46 UTC
    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