in reply to How do I work with multidimensional arrays in Moose?

You could take a look at List::Objects::WithUtils and List::Objects::Types.

(I might pop back later and add an example of using them.)

Update: OK, I'm back! The TypedArray type constraint from List::Objects::Types didn't have quite the features I needed for the example, so I submitted a patch to the module's author, and he's released a new version.

Here's my example now...

use v5.14; package Cell { use Moose; use Types::Standard -types; has name => (is => 'rw', isa => Str); __PACKAGE__->meta->make_immutable; } package Grid { use Moose; use Types::Standard -types; use List::Objects::Types -types; my $CellType = (InstanceOf['Cell'])->plus_coercions( Str, sub { 'Cell'->new(name => $_) }, ); has cells => ( is => 'ro', isa => TypedArray[TypedArray[$CellType]], coerce => 1, handles => { get_row => 'get', set_row => 'set', all_rows => 'all', add_row => 'push', }, ); sub get_cell { my $self = shift; my ($row, $col) = @_; $self->get_row($row)->get($col); } sub set_cell { my $self = shift; my ($row, $col, $value) = @_; $self->get_row($row)->set($col, $value); } sub all_cells { my $self = shift; map { $_->all } $self->all_rows; } sub get_col { my $self = shift; my ($col) = @_; map { $_->get($col) } $self->all_rows; } sub set_col { my $self = shift; my ($col, $values) = @_; my @rows = $self->all_rows; for my $i (0 .. $#rows) { $rows[$i]->set($col) = $values->[$i]; } } sub add_col { my $self = shift; my ($values) = @_; my @rows = $self->all_rows; for my $i (0 .. $#rows) { $rows[$i]->push($values->[$i]); } } sub all_cols { my $self = shift; my $col_count = $self->get_row(0)->count; my $return_type = TypedArray[$CellType]; return map { $return_type->coerce($_); } map { [ $self->get_col($_) ]; } 0 .. $col_count-1; } sub to_string { my $self = shift; join "\n", map(join("\t", map($_->name, $_->all)), $self->all_ +rows); } __PACKAGE__->meta->make_immutable; } my $grid = Grid->new( cells => [ [ 'foo1', 'bar1' ], [ 'foo2', 'bar2' ], ] ); $grid->add_col(['baz1', 'baz2']); $grid->get_cell(1, 1)->name('QUUX'); say $grid->to_string; __END__ foo1 bar1 baz1 foo2 QUUX baz2

Here's an alternative built using my Moops OO framework which I'm currently in pimping mode for. Its load time is about 35% faster than the Moose version above.

use Moops; class Cell { has name => (is => 'rw', isa => Str); } class Grid types Types::Standard, List::Objects::Types { my $CellType = (InstanceOf['Cell'])->plus_coercions( Str, sub { 'Cell'->new(name => $_) }, ); has cells => ( is => 'ro', isa => TypedArray[TypedArray[$CellType]], coerce => 1, handles => { get_row => 'get', set_row => 'set', all_rows => 'all', add_row => 'push', }, ); method get_cell (Int $row, Int $col) { $self->get_row($row)->get($col); } method set_cell (Int $row, Int $col, Str|Object $value) { $self->get_row($row)->set($col, $value); } method all_cells () { map { $_->all } $self->all_rows; } method get_col (Int $col) { map { $_->get($col) } $self->all_rows; } method set_col (Int $col, ArrayRef|ArrayObj $values) { my @rows = $self->all_rows; for my $i (0 .. $#rows) { $rows[$i]->set($col) = $values->[$i]; } } method add_col (ArrayRef|ArrayObj $values) { my @rows = $self->all_rows; for my $i (0 .. $#rows) { $rows[$i]->push($values->[$i]); } } method all_cols () { my $col_count = $self->get_row(0)->count; my $return_type = TypedArray[$CellType]; return map { $return_type->coerce($_); } map { [ $self->get_col($_) ]; } 0 .. $col_count-1; } method to_string () { join "\n", map(join("\t", map($_->name, $_->all)), $self->all_ +rows); } } my $grid = Grid->new( cells => [ [ 'foo1', 'bar1' ], [ 'foo2', 'bar2' ], ] ); $grid->add_col(['baz1', 'baz2']); $grid->get_cell(1, 1)->name('QUUX'); say $grid->to_string; __END__ foo1 bar1 baz1 foo2 QUUX baz2

There is some scope for improvement. For example, the code assumes that each row will hold the same number of cells. (That is, the grid doesn't have a "ragged right edge".) It might be a good idea to assert this in the code some places, to make sure that it's always the case.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name


Comment on Re: How do I work with multidimensional arrays in Moose?
Select or Download Code
Re^2: How do I work with multidimensional arrays in Moose?
by paulymer (Novice) on Sep 03, 2013 at 22:55 UTC

    Thank you, tobyink. I looked at the links, but it appears that they mostly deal with 1D structures (unless I missed something). How would I use these for 2D structures? And yes, an example or two would be much appreciated.

Re^2: How do I work with multidimensional arrays in Moose?
by tobyink (Abbot) on Sep 04, 2013 at 09:53 UTC

    Further to that, here's the Moose example reworked to use MooseX::Role::Parameterized; this factors out the grid-related logic into a role, to make it easier to create classes that are a grid of Cell objects, or a grid of strings, or a grid of filehandles, or whatever...

    I really need to add some sugar for parameterized roles to Moops. :-)

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name