cheekuperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
In how many different ways can an array be displayed?
I can think of:
@arr=(11,22,33,44); print for (@arr); #1 print foreach (@arr); #2 for($idx=0;$idx<scalar(@arr);$idx++) #3 { print $arr[$idx]; } $idx=0; while($idx<scalar(@arr)) #4 { print $arr[$idx]; $idx++; } print join(",",@arr); #5 print "@arr"; #6 print @arr; #7
Are there any other ways in which this can be done?
It may not serve much purpose..but you never know :)

Replies are listed 'Best First'.
Re: TIMTOWTDI - printing an array
by morgon (Priest) on Jun 20, 2012 at 17:21 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

Re: TIMTOWTDI - printing an array
by AnomalousMonk (Archbishop) on Jun 20, 2012 at 18:33 UTC

    As long as you're permuting over the for and foreach synonyms, why not include
        foreach (my $x = 11;  $x < 33;  $x += 11) { print $x }

    And for that matter, how about array references:
        my @array = (11, 22, 33, 44);
        my $array_ref = \@array;
        print for @array;
        print for @$array_ref;
    etcetera.

Re: TIMTOWTDI - printing an array
by johngg (Canon) on Jun 20, 2012 at 19:33 UTC

    Anonymous subroutine perhaps?

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

    Cheers,

    JohnGG

        That also works but you are incurring the expense of creating and invoking the subroutine for each and every element of the array rather than just once. This might have an impact on performance.

        Cheers,

        JohnGG

        I think this benchmark code confirms my point regarding the expense of continually creating and invoking anonymous subroutines.

        use strict; use warnings; use Benchmark qw{ cmpthese }; my @arr = ( 1 .. 1e6 ); my $rcOneElement = sub { my $aref = shift; sub { my $var = shift }->( $_ ) for @$aref; }; my $rcAllElements = sub { my $aref = shift; sub { my $var = shift while @_ }->( @$aref ); }; cmpthese ( -5, { OneElement => sub { $rcOneElement->( \ @arr ) }, AllElements => sub { $rcAllElements->( \ @arr ) }, } );

        The output.

        Rate OneElement AllElements OneElement 2.59/s -- -58% AllElements 6.16/s 138% --

        However, I am good at cocking up benchmarks so I could be deluding myself :-/

        Cheers,

        JohnGG

Re: TIMTOWTDI - printing an array
by frozenwithjoy (Priest) on Jun 20, 2012 at 17:28 UTC
Re: TIMTOWTDI - printing an array
by stevieb (Canon) on Jun 20, 2012 at 17:41 UTC
    $p=japh;push@a,w();$s=j4;sub n{"8fbac6c6e252"};unshift@a, "b4d6c7ea52a7";$k=crypt($s,$p);$o="aeafa7cfdbd58c";@h= map{sprintf"%x",ord$_}split//,$k;push@a,$o;$a[3]=pop@a; $a[2]=n();sub w{"bcb3d8dec8dd"}$x.=$_ for@a;@b=($x=~m/..?/g); push@z,@h until @z>@b;for(@b){push@japh,hex($_)-hex($z [$n]);$n++;}say map{chr$_}@japh;

    ;)

Re: TIMTOWTDI - printing an array
by choroba (Cardinal) on Jun 20, 2012 at 20:46 UTC
    my @arr = (11, 22, 33, 44); open my $FH, '<', \("@arr"); print while <$FH>;
Re: TIMTOWTDI - printing an array
by stevieb (Canon) on Jun 20, 2012 at 23:22 UTC

    In more recent versions of Perl, you can use each():

    my @arr = qw(a b c); while ( my ( $i, $e ) = each @arr ){ say "iter $i = $e"; }
Re: TIMTOWTDI - printing an array
by Tux (Canon) on Jun 21, 2012 at 06:57 UTC
    { local $" = " "; print "@arr'; } print "@{[@arr]}"; print "@{[map{"$_"}@arr]}"; print $arr[-$_-1] for reverse 0..$#arr;

    Enjoy, Have FUN! H.Merijn
Re: TIMTOWTDI - printing an array
by tobyink (Canon) on Jun 21, 2012 at 09:17 UTC
    my @array = qw(foo bar baz); system echo => $_ for @array;

    And if you think the above is insane...

    @array = qw(foo bar baz); use File::Temp; foreach my $string (@array) { my $tmpfile = File::Temp->new; printf $tmpfile <<'HERE' => quotemeta($string); { package Local::App::PrintString; use 5.010; sub run { say "%s"; } } Local::App::PrintString->run(@ARGV) unless scalar caller; HERE system $^X, $tmpfile->filename; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'