Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Sequential Replacement

by cidaris (Friar)
on Jul 09, 2002 at 17:27 UTC ( #180548=perlquestion: print w/replies, xml ) Need Help??

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

I currently have a big, ugly, repetitive, procedural way of doing this:
For every instance of ##REPLACE##, insert the next element of an array.
I was hoping there was some way I could use the \G and //g operators with an s//$rep/ where $rep=shift(@array) operation in order to make it work, but my attempts have failed with unexpected results (sometimes multiple ##REPLACE##(s) in the same line will get ignored, sometimes not.)

Has anyone tried doing something similar, or perhaps someone with much more REGEX experience has input?


Replies are listed 'Best First'.
Re: Sequential Replacement
by elusion (Curate) on Jul 09, 2002 at 17:39 UTC
    Try using the //e modifier with the //g modifier. For instance:
    my @array = (1, 2, 3, 4, 5, 6); my $string = "ababbabaaba"; $string =~ s/a/shift(@array)/ge; print $string; # prints "1b2bb3b45b6"
    The //e modifier executes whatever is between the last to //'s as code.

    elusion :

Re: Sequential Replacement
by Aristotle (Chancellor) on Jul 09, 2002 at 19:16 UTC

    I have a suspicion that what you want to do is to fill a template.. in which case, rather than roll your own solution, you should check CPAN - there's a lot of modules that do a very good job of this:

    Of course, do give the solutions you got from the others a whirl to learn some more about regexen. :-) But for actually solving the task at hand the modules will do a better job in every aspect than a solution that you might write in a sane amount of time would. As a bonus, the "larger" ones of them offer a lot of power, so that you won't "grow out of" them anytime soon. (In fact I don't think one can grow out of TT2 at all..)

    See perrin's templating system roundup at for some of the whys and a more detailed "tour" around some of these modules.

    Makeshifts last the longest.

Re: Sequential Replacement
by DamnDirtyApe (Curate) on Jul 09, 2002 at 17:44 UTC

    This works for me:

    #! /usr/bin/perl use strict ; use warnings ; my @arr = qw/ cidaris see doing / ; my $str = <<'EOD' ; Hello, ##REPLACE##. Nice to ##REPLACE## you. How are you ##REPLACE## today? EOD my $i = 0 ; $str =~ s/##REPLACE##/$arr[$i++]/g ; print $str ;

    D a m n D i r t y A p e
    Home Node | Email
(zdog) Re: Sequential Replacement
by zdog (Priest) on Jul 09, 2002 at 17:53 UTC
    Here's a little different way of doing it that may give you some other ideas:

    use strict; use warnings; my @array = qw (zero one two); my $str = "blah ##REP0## blah ##REP1##\nblah ##REP2##\n"; $str =~ s/##REP(\d+?)##/$array[$1]/g; print $str;

    Update: Another idea is to replace the ##*##'s with %s and do:

    $str = sprintf ($str, @array);

    Just a thought ...

    Zenon Zabinski | zdog |

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://180548]
Approved by little
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2023-03-28 11:06 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (67 votes). Check out past polls.