neversaint has asked for the wisdom of the Perl Monks concerning the following question:
Using $delim, I wish to identify region in $array. First of all we can decompose $delim into sub-regions.$delim = 'A -3 C -4 B'; $array = ['A -4 A','A -1 C','C -4 D','D -4 B','B -3 C','C -2 B','B -1 E']; + * + * + * + * + * + * # Note that in $array, starting from # second element $array->[1] until $array->[-1], # the first alphabet ( asterisks *) is actually # a repeat of the last alphabet ( plus +) # from the previous element.
my $delim = 'A -3 C -4 B'; my $delim_reg = ['A -3 C','C -4 B'];
Let me try to describe the picture above. For example, Instance 1 contains two sub-regions (i and j) which correspond to each marker from $delim_reg. So in "i" a sub-region is started with A and ended with C. Similarly in "j" a sub-region is started with C and ended with B.my $delim_reg = ['A -3 C','C -4 B']; \__i__/ \__j__/ --> sub-regions my $array = ['A -4 A','A -1 C','C -4 D','D -4 B','B -3 C', 'C -2 B','B -1 E']; |\_____i_______/ \__j__/|-> Inst 1 |____________________________________________________| |\_i__/ \_____j________/ |-------> Instance 2 |_________________________| ... till |\_____i_______/ \_______________j__________________/ |--> Inst 6 |______________________________________________________|
I need to keep the result in forms of AoA, so that I can later add other value into it. In other cases, $delim can contain more or less than 3 alphabets. The size of the $array may also be varied. Thus generating more/lesser sub-regions and number of instances too.my $VAR = [ [['A -4 A', 'A -1 C'], ['C -2 B']], # Ins1 [['A -4 A', 'A -1 C'], ['C -4 D','D -4 B']], # Ins2 [['A -4 A', 'A -1 C'], ['C -4 D','D -4 B','B -4 C','C -2 B']], # Ins3 [['A -4 A', 'A -1 C', 'C -4 D','D -4 B','B -4 C'],['C -2 B']], # Ins4 [['A -1 C'], ['C -4 D', 'D -4 B']], # Ins5 [['A -1 C'], ['C -4 D', 'D -4 B','B -3 C','C -2 B']], # Ins6 ]
use Data::Dumper; my $delim = 'A -3 C -4 B'; my $array = ['A -4 A','A -1 C','C -4 D','D -4 B','B -3 C','C -2 B','B -1 E']; get_delim_region($delim,$array); sub get_delim_region{ my ($delim,$array) = @_; my $delim_reg = decomp_str($delim); # I'm really stuck from here..... my @instances; OUT: foreach my $delim_rg ( @{$delim_reg} ){ my @delimreg; my $st1 = (split(" ",$delim_rg))[0]; my $st2 = (split(" ",$delim_rg))[2]; IN: foreach my $i ( 0 .. @{$array}-1 ){ my $tr1 = (split(" ",$array->[$i]))[0]; my $tr2 = (split(" ",$array->[$i]))[2]; if($st1 eq $tr1){ push @delimreg,$array->[$i]; } elsif($st2 eq $tr2){ push @delimreg,$array->[$i]; next OUT; } push @instances, [ @delimreg ]; } } print Dumper \@instances; return ; } sub decomp_str{ # This subroutine decompose a string into sub-regions i.e # from: $delim = 'A -3 C -4 B'; # into: $delim_reg = ['A -3 C','C -4 B']; # Credit Roy Johnson - fastest [$_[0] =~ /(?=([a-z]\s*(?:\S+\s*){2}))\S+\s*/gi ] }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Identifying Delimited Regions of an Array
by blokhead (Monsignor) on Oct 15, 2005 at 20:59 UTC | |
by neversaint (Deacon) on Oct 16, 2005 at 09:08 UTC | |
|
Re: Identifying Delimited Regions of an Array
by Skeeve (Parson) on Oct 15, 2005 at 21:02 UTC |