http://www.perlmonks.org?node_id=1211262

Started with perl6 shortly.. perl5 has AoA ... wanted to know if something can be done in perl6...
It seems that it can be done using perl6 :

C:\WINDOWS\system32>perl6 -v This is Rakudo Star version 2018.01 built on MoarVM version 2018.01 implementing Perl 6.c.

So here is the code which works under windows10

use v6; my @arr = [ [ 1.1,2.2,3.3,4.4,5.5 ], [ 10,20,30,40,50 ], [ 100,200,300,400,500 ], [ 1000,2000,3000,4000,5000 ], ]; dd @arr; # dump the matrix loop ( my $row=0; $row <= @arr.end; $row++) { #say "Idx : $row"; loop (my $col=0 ; $col <= @arr[$row].end ; $col++ ) { print "@arr[$row][$col].fmt("%7.1f")\t"; } print "\n"; } my $aant_cols = ( @arr[0].end ) ; # It's a matrix : so Just take one +row to find out the number of columns # cannot use @arr[0].elems : gives +an error print "=======\t" x $aant_cols + 1 , "\n"; loop ( my $col=0 ; $col <= $aant_cols ; $col++ ) { printf "%7.1f" , [+] @arr[*;$col] ; # calculate the total for each + column print "\t" } say "";

The above creates the following result

Array @arr = [1.1, 2.2, 3.3, 4.4, 5.5, 10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000]
    1.1     2.2     3.3     4.4     5.5
   10.0    20.0    30.0    40.0    50.0
  100.0   200.0   300.0   400.0   500.0
 1000.0  2000.0  3000.0  4000.0  5000.0
======= ======= ======= ======= =======
 1111.1  2222.2  3333.3  4444.4  5555.5

Please notice : All columns are totalled ( =sum in excel ) .

Could not find any usefull examples in the perl6 documentation. So I learned it myself.
Hope it's of use for sombody else

Have fun with perl6

Replies are listed 'Best First'.
Re: perl6 matrix arrayof arrays
by mr_ron (Chaplain) on Mar 22, 2018 at 13:24 UTC
    cannot use @arr[0].elems : gives an error

    Like @arr vs $#arr in Perl, I'm guessing @arr[0].elems was causing warnings for accessing the element just after the end of an array.

    A possible alternative coding:

    use v6; my @arr = [ [ 1.1,2.2,3.3,4.4,5.5 ], [ 10,20,30,40,50 ], [ 100,200,300,400,500 ], [ 1000,2000,3000,4000,5000 ], ]; dd @arr; # dump the matrix for @arr { say ( .map: {.fmt: "%7.1f\t"} ).join } say "=======\t" x @arr[0].elems; say ( (0..@arr[0].end).map: {@arr[*; $_].sum.fmt("%7.1f\t")} ).join; #say ( ([Z] @arr).map: { .sum.fmt("%7.1f\t") } ).join;
    Ron
      hello Ron

      Thanks for you're reply. As i said : just starting with perl6.
      Learning from the docs is not easy. But what you showed me was easy to understand

      wanted to use the + in calculating the totals of each column. The way with sum is new to me.
      Thanks for showing this option to do it an other way..

        Since you describe the "docs" as "not easy", I am concerned that you are only looking at docs.perl6.org and not also looking at the tutorial level information at perl6.org/resources. Have you looked at tutorials or introductory books?

        A review of the tutorials led me to refine:

        for @arr { say ( .map: {.fmt: "%7.1f\t"} ).join }
        to be
        for @arr { say ($_>>.fmt: "%7.1f\t" ).join }
        Ron
Re: perl6 matrix arrayof arrays
by b2gills (Novice) on Mar 28, 2018 at 16:31 UTC

    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(); } }