When you get used to the way functions work with lists, you might start writing it more like the following.
(Then again maybe not; it could be that I'm just really used to doing code golfs.)

# calls .fmt on each array which then calls .fmt on the elements # the results of which are implicitly joined with a space # print the result with trailing newline .fmt("%7.1f").put for @arr; # if it must be tab separated # $_».fmt("%7.1f").join("\t").put for @arr; # repeat '=' as a string 7 times, and list repeat that # coerce to Str (space separated) and print with newline put '=' x 7 xx @arr[0]; # if it must be tab separated # put ('=' x 7 xx @arr[0]).join("\t"); # add the values in the columns up and format them put [Z+](@arr).fmt("%7.1f"); # if it must be tab separated # put [Z+](@arr)».fmt("%7.1f").join("\t");

At the very least I wouldn't use loop for most purposes, as it's easy to get off-by-one errors and to accidently mutate the iterator variable in the block. Also the iterator variable in a loop construct is scoped to the outer block, not the loop block.

for @arr -> @inner { for @inner -> $_ { print .fmt("%7.1f\t"); } put(); # use the value of $*OUT.nl-out } ... for [Z+] @arr { print .fmt("%7.1f\t"); LAST put(); } }

In reply to Re: perl6 matrix arrayof arrays by b2gills
in thread perl6 matrix arrayof arrays by teun-arno

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.