Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi
Imagine I have a string:
$thisstring = "abjdkwosyksljadfkjadfaduifalkdjfaslflksdf";
Now assume that "abjdk" and "adfkj" can be the equivalent of "H" (I can easily write my own sub to evaluate whether a given part of the string is an equivalent) then i'd like to be able to do something like:
$thisstring =~ /H/;
Is there any way to do this. Ideally I suppose i'd like to put a function instead of the H. If I can do this its going to make my cunning task extremely simple.

Thanks
Specimen

Replies are listed 'Best First'.
Re: is this a regexp question ?
by chipmunk (Parson) on Jun 04, 2001 at 05:35 UTC
    Your question is a bit unclear. How do you determine whether a given part of the string is equivalent to "H"?

    This may be something like what you want:

    $thisstring = "abjdkwosyksljadfkjadfaduifalkdjfaslflksdf"; sub H { return qr/abjdk|adfkj/; } if ($thisstring =~ H) { print "Yes!\n"; } else { print "No.\n"; }
Re: is this a regexp question ?
by bikeNomad (Priest) on Jun 04, 2001 at 02:39 UTC
    I'm afraid I don't understand your question. But, as usual, that won't stop me <g>!

    You can factor out bits of a regular expression, and pre-compile them using the qr() operator:

    my $H = qr{abjdk|adfkj}; if ($thisstring =~ $H) { # do stuff }

    If this isn't what you mean, please clarify.
Re: is this a regexp question ?
by jorg (Friar) on Jun 04, 2001 at 04:42 UTC
    I'm not sure whether you can eval a function in a regex. If I understand your question correctly, you can just do
    sub search_equivalent{ #do stuff here and return your equivalent pattern return $equivalent; } my $equiv = search_equivalent(); if ($thisstring =~ m/$equiv/){ .... }
    There is no real need to put a function call in the regex.

    Jorg

    "Do or do not, there is no try" -- Yoda
      Even though this is probably not what he meant, you can call a function from within a code-block within a regex:
      $foo =~ /(?{match_foo()})/;

      ar0n ]

Re: is this a regexp question ?
by Anonymous Monk on Jun 04, 2001 at 21:10 UTC
    Hi
    Sorry for not getting back sooner.
    OK, a practical example of what I want to do. Lets say that every letter in the following string represents a number from 0 to 100 or whatever.
    $thisstring = "abjdkwosyksljadfkjadfaduifalkdjfaslflksdf";
    
    now lets say that the letter H represents some function, I would want
    
    $thisstring =~ /H/;
    
    
    to match all the sequences of 5 or fewer numbers that were equal to 10. Clearly writing the function H is trivial in this instance, and I would be able just to go shooting through the string and compute this without recourse to a regexp but for more complex examples (where for example there are gaps between them), eg 3 numbers less than 10 followed by any 5 numbers followed by 4 numbers equal in sum to 10 - stuff like that.
    Am I talking rubbish or can perl offer me a shortcut to doing this ?
    Thanks
    Specimen