Dear blokhead,

Thank you so much for your -life saving- answer to my posting. I managed to get it work on larger test case.

There are couple of critical issues however, that I truly hope you won't mind to take a look at it. Because I honestly don't know how to go about resolving it.

My code below already give correct answers (98%), except for these two cases that give unexpected result:

Case 1
Given $delim = = 'A -3 C -4 B'; $set->{'key2'} => ['A -4 B','B -4 C','C -4 A','A -4 B'], RESULT: key2 [A -4 B] [B -4 C] / [A -4 B] [B -4 C] / [C -4 A] [A -4 B] # only this is correct
Given that input, only the latter is the answer, because both sub-region are satisfied (covered).

Case 2
Given: $delim2 = 'A -4 B'; $set->{'key2'} => ['A -4 B','B -4 C','C -4 A','A -4 B'], RESULT: [A -4 B] #ok [A -4 B] #ok [A -4 B] [B -4 C] [C -4 A] [A -4 B] #ok [A -4 B] #not ok -- don't know how this is obtained
I can understand where the first 3 lines come from - and they are correct, but not the last one.
My full running code based on your approach is as follows:
#!/usr/bin/perl -l use warnings; 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 -2 B','B -1 E'], 'key2' => ['A -4 B','B -4 C','C -4 A','A -4 B'], 'key3' => ['A -4 C','C -4 B','B -4 A','A -2 C','C -3 B'], }; my $delim = 'A -3 C -4 B'; my $delim2 = 'A -4 B'; get_region($delim,$set); sub get_region { my ($delim,$set) = @_; OUT: foreach my $setkey ( sort keys %{$set} ) { print "$setkey\n"; no warnings q/uninitialized/; # Do you know how can I remove warning # from the code below? 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 { 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 ]; } ); # As per your suggestion, I do want to use deep # interval, because later I want to test each $interval # with certain condition and then do "next OUT" # ---- as speed-up heuristic foreach my $int (@intervals ) { if (@{$int->[0]}) { print join " / ", map { my $range = $_; join " ", map "[@$_]", @$range } @{$int}; my $numtpl = num_of_tupl($int); #next OUT if ($numtpl >= 5); } } print "\n"; } return ; } 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; } # These two subroutines # split the string token into pure array, as you suggested sub decomp_str2aoa { return decomp_a2aoa([$_[0] =~ /(?=([a-z]\s*(?:\S+\s*){2}))\S+\s*/g +i ]); } sub decomp_a2aoa { my $arr = shift; return map{ [split(" ",$_)] }@{$arr}; }
As a side note, It never occur to me to view this problem as a "routing problem". If it resemble it, will it be helpful? My concern is speed. Do you think using some graph module which you briefly mention can be helpful/ speed-up the code? In particular I need to run this function in hundred thousands of cases.

I apologize for having taking much of your time to answer my postings. I try not to trouble you again after this.


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

In reply to Re^2: Identifying Delimited Regions of an Array by neversaint
in thread Identifying Delimited Regions of an Array 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.