Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Multidimensional arrays

by baxy77bax (Deacon)
on Mar 26, 2020 at 15:13 UTC ( [id://11114679]=perlquestion: print w/replies, xml ) Need Help??

baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Hi

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:

sub 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);
but I keep getting  $VAR1 = []; What am i missing ?? Is there a better way to do this ? thnx

Replies are listed 'Best First'.
Re: Multidimensional arrays
by tybalt89 (Monsignor) on Mar 26, 2020 at 17:04 UTC

    There are other ways, too...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11114679 use warnings; use Data::Dump qw(pp dd); my $A = [[1,2],[2,3]]; my $B = eval pp $A; sub add1 { ref $_[0] ? [ map add1($_), @{$_[0]} ] : $_[0] + 1 } my $incremented = add1( $A ); dd $A; dd $B; dd $incremented;

    Outputs:

    [[1, 2], [2, 3]] [[1, 2], [2, 3]] [[2, 3], [3, 4]]
Re: Multidimensional arrays
by hippo (Bishop) on Mar 26, 2020 at 15:20 UTC
    Is there a better way to do this?

    Why not use dclone like the FAQ suggests?

      Because i did not know about it ... :( Thnx!!!!!!
Re: Multidimensional arrays
by vr (Curate) on Mar 27, 2020 at 10:02 UTC

    To pinpoint where your code fails: at the time you recurse, the $out->[$i] is undefined. This "undefined" is assigned to lexical $out in callee, and further assignment e.g. $out->[$i] = $in->[$i] autovivifies $out as array reference, but unfortunately it has no connection whatsoever with $out in caller. To fix, replace

    $self->_recurse($in->[$i], $out->[$i]);

    with

    $self->_recurse($in->[$i], $out->[$i] //= []);

    Plus, if (!$out->[$i]) should be written if (!defined $out->[$i]), or you'll never increment zero values.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114679]
Approved by hippo
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found