in reply to Replace placeholders in text in an elegant manner
And in a few seconds Perl just made a new masterpiece called "CountZero and Elisabeth".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)
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replace placeholders in text in an elegant manner
by explodec14 (Novice) on Feb 27, 2010 at 20:32 UTC | |
by GrandFather (Saint) on Feb 28, 2010 at 00:15 UTC |