I have this simple input
I have {red;green;orange} fruit and cup of {tea;coffee;juice}
I use Perl to identify patterns between two external brace delimiters {
and }
, and randomize the fields inside with the internal delimiter ;
.
I'm getting this output
I have green fruit and cup of coffee
This is my working Perl script
perl -plE 's!\{(.*?)\}!@x=split/;/,$1;$x[rand@x]!ge' <<< 'I have {red;green;orange} fruit and cup of {tea;coffee;juice}'
My task is to process this input format
I have { {red;green;orange} fruit ; cup of {tea;coffee;juice} } and {nice;fresh} {sandwich;burger}.
As I understood, the script should skip external closing braces { ... }
in the first text part, which has text inside with opening and closing brackets:
{ {red;green;orange} fruit ; cup of {tea;coffee;juice} }
It should choose a random part, like this
{red;green;orange} fruit
or
cup of {tea;coffee;juice}
Then it goes deeper:
green fruit
After all text is processed, the result may be any of the following
I have red fruit and fresh burger.
I have cup of tea and nice sandwich
I have green fruit and nice burger.
I have cup of coffee and fresh burger.
The script should parse and randomize the next text too. For example
This {beautiful;perfect} {image;photography}, captured with the { {NASA;ESA} Hubble Telescope ; {NASA;ESA} Hubble Space Telescope} }, is the {largest;sharpest} image ever taken of the Andromeda galaxy { {— otherwise known as M31;— known as M31}; [empty here] }.
This is a cropped version of the full image and has 1.5 billion pixels. { You would need more than {600;700;800} HD television screens to display the whole image. ; If you want to display the whole image, you need to download more than {1;2} Tb. traffic and use 800 HD displays }
An example output could be
This beautiful image, captured with the NASA Hubble Telescope, is the
sharpest image ever taken of the Andromeda galaxy — otherwise known as
M31.
This is a cropped version of the full image and has 1.5 billion
pixels. You would need more than 700 HD television screens to display
the whole image.
srand
? – Niles