| nobull67@gmail.com 2006-10-31, 3:57 am |
|
On Oct 30, 10:55 pm, "Rick" <rick.peng...@gmail.com> wrote:
> Hi, guys
>
> I need to loop over a string to find some pattern A, do some
> computation according to A and replace A with B. I am trying to do this
> within a while loop but I have no idea how to plug s///g into this
> loop. Can anyone offer some clue?
Clue: s///g _is_ a looping constuct.
> Here is an example: In string 'a123ba456b', the pattern to find is
> a(.*?)b and this string needs to be processed as 'a(1)ba(2)b', meaning
> that the occurance of '123' is recorded and put back into the original
> string.
I don't know what you mean by that.
If you want to save the matching patterns to an array and put an index
in parentheses in place of the saved string.
s/(a)(.*?)(b)/$1(@{ push @save, $2; [ scalar @save ]})$3/g;
OR
s/(a)(.*?)(b)/ push @save, $2; "$1(" . @save . ")$3" /eg;
|