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


In reply to Re: Re: Pyramid: an interesting use for x by tachyon
in thread Pyramid: an interesting use for x by tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.