eibwen has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to run a few lines of code on each instance of a string of a particular form. To accomplish this I've been using a regex of the form:
$s =~ /(string)(?{ print $^N, $/; })(?!)/;
While this works to print all occurances of the string in question (see Global Regex sans //g), I cannot seem to get this to work in list (or perhaps array) context:
my @letters = ('a'..'z'); for my $letter (0..$#letters) { $letters[$letter] =~ /(?{ print $letter })(?!)/; }
The code seems to run each iteration; however instead of printing the current value of $letter, it returns the following error:
Use of uninitialized value in print at (re_eval 1) line 1.
As I type this I wonder if the regex and the embeded code are being compiled prior to the instantion of the variable? I tried running it through Deparse, but it appeared to be more or less unaltered. Should this be the case, I'd surmise it happens afterwards -- perhaps at runtime?
How can I print the current value of the iteration from within the regex -- preferrably sans eval?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Iterating over code embeded in regex
by ikegami (Patriarch) on Jun 13, 2006 at 03:47 UTC | |
|
Re: Iterating over code embeded in regex
by Samy_rio (Vicar) on Jun 13, 2006 at 04:01 UTC | |
|
Re: Iterating over code embeded in regex
by ioannis (Abbot) on Jun 13, 2006 at 05:34 UTC | |
|
Re: Iterating over code embeded in regex
by ambrus (Abbot) on Jun 13, 2006 at 12:31 UTC |