Need to set an entire 2D array in one line, then print it all out with another one liner?

If not this code is totally pointless!

tachyon

my@a=([],[],[],[],[],[],[],[],[],[]); my$a=-1;map{@$_=(++$a)x$a}@a; map{map{print "-",$_}@$_;print"\n"}@a;

Replies are listed 'Best First'.
Re: Pyramid: an interesting use for x
by MeowChow (Vicar) on May 25, 2001 at 05:59 UTC
    Perhaps I'm missing the point(lessness)?
    print-$_ x$_,$/for 1..9
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

      Yes, I think you are. Certainly the result of this code is trivial to reproduce with a print, and fairly pointless.

      *BUT* I think how this occurs in my code is rather intriguing. We are certainly not simply printing. What happens is that we populate the second dimension of our 2D array using 'x' to control the number of elements (in this case we increment the number of elements from 0 to 9). We then dump the whole 2D data structure in a one liner. Consider the original code, now commented with a function to make it plain what occurs:

      my@a=([],[],[],[],[],[],[],[],[],[]); print_2D_array(@a); my$a=-1;map{@$_=(++$a)x$a}@a; print_2D_array(@a); map{map{print "-",$_}@$_;print"\n"}@a; sub print_2D_array { my $count; for my $ref (@a) { my @inner_array = @$ref; print "Elements of level ".$count++." "; for(@inner_array) {print "$_-"} print "\n"; } print "\n"; } print "\n\nDon't be a square\n\n"; my@a=([],[],[],[],[],[],[],[],[],[]); my$a=-1;map{@$_=('no')x10}@a; map{map{print "-",$_}@$_;print"\n"}@a;

      What I find most interesting is summarised by this code:

      my $count; @arr = (1) x 20; print "Element ".$count++.":$_\n" for @arr;

      tachyon