baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
Let say I have a multidimensional array and every time the number of dimensions is different. I don't have apriori information about the number of dimensions thus i need to recursively access each and figure out if there is more depth or not. But I also need to copy this array into another and modify it (let say increment). Curently I am trying to do it like this:
but I keep getting $VAR1 = []; What am i missing ?? Is there a better way to do this ? thnxsub new { my ($class) = @_; my $self->{_mtx_} = []; bless $self, $class; return $self; } sub copycomp_matrix { my ($self, $arg) = @_; $self->_recurse($arg,$self->{_mtx_}); print Dumper($self->{_mtx_}); } sub _recurse { my ($self, $in, $out) = @_; for (my $i = 0;$i<@{$in};$i++){ if (ref $in->[$i] eq 'ARRAY'){ $self->_recurse($in->[$i], $out->[$i]); }else{ if (!$out->[$i]){ $out->[$i] = $in->[$i] ; }else{ $out->[$i] = $in->[$i] +1 ; } } } } ######################################################### use strict; use lib "./"; use Above::Obj; my $z = Above::Obj->new(); my $a = [[1,2],[2,3]]; $z->copycomp_matrix($a);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multidimensional arrays
by tybalt89 (Monsignor) on Mar 26, 2020 at 17:04 UTC | |
|
Re: Multidimensional arrays
by hippo (Archbishop) on Mar 26, 2020 at 15:20 UTC | |
by baxy77bax (Deacon) on Mar 26, 2020 at 15:33 UTC | |
|
Re: Multidimensional arrays
by vr (Curate) on Mar 27, 2020 at 10:02 UTC |