Create a two-dimensional array at runtime

Author: Salve J. Nilsen

Get two integers from the user, then create a two-dimensional array where the two dimensions have the sizes given by those numbers, and which can be accessed in the most natural way possible. Write some element of that array, and then output that element. Finally destroy the array if not done by the language itself.


Source code:

use v6;

my ($major, $minor) = prompt("Dimensions? ").comb(/\d+/);
die "Please enter two dimensions" unless $major && $minor;

my @array := [ for ^$major { [ for ^$minor { '@' } ] } ];

@array[ (^$major).pick  ][ (^$minor).pick ] = ' ';

.Str.say for @array;