neversaint has asked for the wisdom of the Perl Monks concerning the following question:
And the sample problematic answer is this:#.... 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
Below is my full code:$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]]] ];
#!/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}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem in Accumulating Deep Array References
by fishbot_v2 (Chaplain) on Oct 17, 2005 at 11:55 UTC | |
|
Re: Problem in Accumulating Deep Array References
by Anonymous Monk on Oct 17, 2005 at 11:20 UTC |