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

Hello everyone-

I have read file into memory and I would like to replace some strings in it (i call them placeholders) with some others of my own. I would like know if there is a way to do this with a single line which will include all the needls/placeholders and all the replacements. I mean without run the following on each placeholder:

$my_text =~ s/some_placeholder/the_replace/g

Replies are listed 'Best First'.
Re: Replace placeholders in text in an elegant manner
by CountZero (Bishop) on Feb 27, 2010 at 20:12 UTC
    Or perhaps something like this?
    use strict; use warnings; my $text = join '', <DATA>; my %replacements = ( 'Romeo' => 'CountZero', 'Juliet' => 'Elisabeth', 'Rom.' => 'CountZero', 'Jul.' => 'Elisabeth', 'Abram' => 'Igor', 'Abr.' => 'Igor', ); $text =~s/(Romeo|Juliet|Rom\.|Jul\.|Abram|Abr\.)/$replacements{$1}/gi; print $text; __DATA__ (follows the full Project Gutenberg e-text of Shakespeare's Romeo and +Juliet; found at http://www.gutenberg.org/dirs/etext97/1ws1610.txt)
    And in a few seconds Perl just made a new masterpiece called "CountZero and Elisabeth".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      You're right on the money!!
      Couldn't be more precise!!

      You're way of solution realy amazed me. Thank you very much.

      PS: I loved your Geoffrey James quotation too!

        There is a gotcha with the solution as written (well, several actually) - you need to provide every possible (or at least likely) case variation for each of the matched words. Combining the special markup you have alluded to elsewhere with the e flag for the regex gives a somewhat more robust solution:

        use strict; use warnings; my $text = join '', <DATA>; my %replacements = ( 'romeo' => 'CountZero', 'juliet' => 'Elisabeth', 'rom.' => 'CountZero', 'jul.' => 'Elisabeth', 'abram' => 'Igor', 'abr.' => 'Igor', ); $text =~s/%%([^%]+)%%/replace($1)/ge; print $text; sub replace { my ($needle) = @_; return "%%$needle%%" if ! exists $replacements{lc $needle}; return $replacements{lc $needle}; } __DATA__ (follows the full Project Gutenberg e-text of Shakespeare's %%Romeo%% +and %%Juliet%%; found at http://www.gutenberg.org/dirs/etext97/1ws1610.txt)

        Prints:

        (follows the full Project Gutenberg e-text of Shakespeare's CountZero +and Elisabeth; found at http://www.gutenberg.org/dirs/etext97/1ws1610.txt)

        True laziness is hard work
Re: Replace placeholders in text in an elegant manner
by repellent (Priest) on Feb 28, 2010 at 03:22 UTC
    Use a template.
    use Text::Template; my $template = Text::Template->new( type => "FILEHANDLE", source => *DATA, ); my $text = $template->fill_in( hash => { placeholder1 => "chicken", placeholder2 => "dog", }, ); print $text; __END__ This is text with { $placeholder1 }. It can be with { $placeholder2 } too. { $placeholder1 } can appear more than one time in the text.
      Aw come on! Don't spoil it for the OP.

      Reinventing a web-framework or a templating system must be one of the rites of passage of becoming a real Perl programmer.

      ;-)

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Replace placeholders in text in an elegant manner
by ww (Archbishop) on Feb 27, 2010 at 19:25 UTC

    Do you mean something like

    $my_test =~ s/placehldr1|placehldr2|placehldr3/sole replacement/

    or is each unique placeholder to be replaced by a unique replacement? (and what is the "needls/" in your narrative?)

    On asking for help and How do I post a question effectively? and you may also want to search for "I know what I mean..."

      needles is a synonim for placeholders.

      Each placeholder has a unique replacement.
      The text can be like the following

      This is text with %%placeholder1%%.
      It can be with %%placeholder2%% too. %%placeholder1%% can appear more than one time in the text.
      My intention is to replace them with my replacements at one line and not to loop on the placeholders and replace them one by one.

Re: Replace placeholders in text in an elegant manner
by AnomalousMonk (Archbishop) on Feb 28, 2010 at 05:46 UTC