in reply to Regexes loaded from file

Seems easy enought to do. Open the file of regexes, open the file you want to change, then read a line from both files, do your substitution and write it.

My question is: Why you'd want to do this? Homework assignment?

Replies are listed 'Best First'.
Re: Re: Regexes loaded from file
by arunhorne (Pilgrim) on May 02, 2002 at 18:19 UTC
    No not homework - needed for felxible processing of a set of metabolite reaction equations. Its not as simple as you allude becuase you need either to use closures or subroutine refs... I just wondered if anyone knew how. Cheers :)
      Sorry, thought it sounded simpler than it was. :)

      However I did do something similar myself not too long ago for a program I wrote. This was for a regular expression tutor I wrote ( which I hope to post to craft as soon as I iron out one blasted bug ) so hopefully this will prove useful to you.

      =head2 If we've had a regular expression with grouping, we need to sho +w the matches we made there too. Unless we count the number of matchi +ng parens, which could get odd if there was a match looking for '( +' and ')', we run a simple counter and check if the match variable exists. + If it does, we print it out. If no parenthesis are used, $match_resul +ts holds our match. =cut {eval "\@matches = (\$user_text =~ $regex); \$match_results .= +\$&;"; $match_results .= "\n"; $match_results .= " Variables:\n"if defined($matches[$i]) +; for (my $i = 1; $i <= @matches; $i++) { my $found = $matches[$i - 1]; $match_results .= " \$$i:$found \n" if defined($matc +hes[$i]); } } # end eval block } # end if substr eq 'm'

      This does encompass multiple matches on an m//. The s// and tr// I did like: eval "\$match_results =~ $regex

      Of course I did have the help of some here to pull this stunt off :)

      Good luck!

      Some sort of <code>while(<>){push @subs,eval 'sub{local$_=shift;'.$_.';$_}'}<code>?
        This is correct but not maximally efficient. Why create a separate sub for each s///? It's more efficient to just build one sub containing all the substitutions. Also, why localize and return $_? Operating on it directly is more efficient. See my solution below for an example of what I'm talking about.

        -sam