I am still dealing with the same problem. 
Rob has suggested me a good solution for macthing consecutive patterns like
 H K D  but not more looser ones like for K[ED]{3,5}?  L.{3}A 
andn my poor perl knowledge doesn't help me to generalize it: /

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

    $_ = "abracadabra";
        @all = m:any /a.*?a/;
produces:

    abra abraca abracada abracadabra aca acada acadabra ada adabra abra
 Is there a version available that supports this structure?
Or are there any creative ideas of Perl wizards?
Thanks in advance
cheers 
oznur
Öznur Tastan wrote:
>
> Your suggestion was quite helpful but I got stuck when I try to modify it for
> general purpose. May be you will have an idea and want to help.
>
> The foreach $n( 1...length sequence) solves the combinatorial problem but the
> combinations can happen in second or third cases
>
> so I need to repeat the foreach loop
> foreach $m(1..length $sequence){
>     foreach $n(1..length sequence){
>           foreach $f(1..length sequence){
>             foreach $r(1..length sequence){
>             next unless $sequence =~ /> (.{$m})H(,{$n})K(.{$f})D(.{$R})/;
>
> But the string was an example an in general case there can be n letters of just
> like H K and D so I need n+1 for each loops repeated and I don't have any idea
> how to write a foreach loop that can should be repeated n times.

Hi Öznur.

Yes, I did suspect my previous answer wouldn't handle the general case, but
I hoped it may be good enough. I'm sure it's not possible using a simple regex.

I've written subroutine split_list() below, which takes a string of characters to
split on and a string to split, a little like the split() built-in. It finds all
the ways to split the string at each of the characters in sequence.

It's a recursive subroutine to make it neater. It works by finding a way to
split on the first character in the list and then calling itself to split
the right-hand half on the remaining characters.

The return value is an array of all the possibilities with the split
characters replaced with hyphens.

Post to the list if you need any help with any part of it.

(Thanks, this was an interesting little problem!)

Cheers,

Rob



use strict;
use warnings;

sub split_list {

  my ($list, $string) = @_;

  return ($string) unless $list =~ s/(.)//;
  my $split = $1;

  my @splits;

  while ( $string =~ /$split/g ) {

    my $pos = pos $string;
    my $left = substr $string, 0, $pos - 1;
    my $right = substr $string, $pos;

    push @splits, map "$left-$_", split_list($list, $right);
  }

  return @splits;
}

my @ret = split_list ('HKD', 'xHxxHyyKzDt');

print map "$_\n", @ret;


**OUTPUT

x-xxHyy-z-t
xHxx-yy-z-t




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to