Dear Masters,

I am trying to accumulate a deep array reference into another array after passing them into certain conditional test.

The problem I have is that while printing line by line the deep reference it prints nicely. It's only after I accumulate these array references, Dumper prints gave a strange result. Why is that? I think my construct is already correct. Here is the portion of the code that gave problem I encounter:
#.... my @accum; foreach my $int (@intervals ) { if (num_of_tupl($int) >= 4) { print Dumper $int; # It prints correctly in this line push @accum,$int; } } print "ACCUM: "; print Dumper \@accum ; # Problem here
And the sample problematic answer is this:
$VAR1 = [ [['A','3','C']], [['C','-4','B'],['B','-4','A'],['A','3','C'],['C','-4','B']] ]; $VAR1 = [ [['A','3','C'],['C','-4','B'],['B','-4','A'],['A','3','C']], [['C','-4','B']] ]; # Problem begins here ACCUM BOTH ABOVE: $VAR1 = [[ [['A','3','C']], [['C','-4','B'],['B','-4','A'],['A','3','C'],['C','-4','B']]], [[$VAR1->[0][0][0], $VAR1->[0][1][0], $VAR1->[0][1][1], $VAR1->[0][1][2]], [$VAR1->[0][1][3]]] ];
Below is my full code:
#!/usr/bin/perl -lw use strict; use Algorithm::Loops 'NestedLoops'; use Data::Dumper; $Data::Dumper::Indent = 0; my $set = { 'key1' => ['A -4 A','A -1 C','C -4 D','D -4 B','B -3 C','C -4 B','B -1 E'], 'key2' => ['A -1 B','B -1 C','C -4 A','A -5 B'], 'key3' => ['A 3 C','C -4 B','B -4 A','A 3 C','C -4 B'], }; my $delim = 'A 3 C -4 B'; get_region_wth_idx($delim,$set); sub get_region_wth_idx { my ($delim,$set) = @_; my %hash; OUT: foreach my $setkey ( sort keys %{$set} ) { print "\n"; print "$setkey\n"; my @delim = decomp_str2aoa($delim); my @data = decomp_a2aoa($set->{$setkey}); my $N = $#data; my $D = $#delim; my @intervals = NestedLoops( [ [0 .. $N], sub {[$_ .. $N]}, ( sub {[$_+1 .. $N]}, sub {[$_ .. $N]} ) x $D ], { OnlyWhen => sub { return 0 if @_ < 2*@delim; for my $d (0 .. $D) { if ($data[ $_[ $d*2 ] ][0] ne $delim[$d][0] or $data[ $_[ $d*2 + 1 ] ][2] ne $delim[$d][2]) { return 0; } } return 1; } }, sub { return [ map { [@data[ $_[2*$_] .. $_[2*$_+1] ]] } 0 .. $D ]; } ); my @accum; foreach my $int (@intervals ) { if (num_of_tupl($int) >= 4) { print Dumper $int; push @accum,$int; } } print "ACCUM BOTH ABOVE: "; print Dumper \@accum ; } # $hash{$setkey} = [ @accum ]; # print Dumper \%hash ; print "\n"; return ; } #--------- Additional subroutine ------- sub num_of_tupl { # Just a simple code, counting # how many tuples (e.g.["A", -3, "C"] ) contain in AoA my $ar = shift; my $count; foreach my $lvl1 ( @{$ar} ) { my $sizein = scalar(@{$lvl1}); $count += $sizein; } return $count; } sub decomp_str2aoa { return decomp_a2aoa([$_[0] =~ /(?=([a-z]\s*(?:\S+\s*){2}))\S+\s*/gi]); } sub decomp_a2aoa { my $arr = shift; return map{ [split(" ",$_)] }@{$arr}; }


---
neversaint and everlastingly indebted.......

In reply to Problem in Accumulating Deep Array References by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.