Created
November 18, 2011 09:20
-
-
Save vzarytovskii/1375984 to your computer and use it in GitHub Desktop.
Randomize phrase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use warnings 'all'; | |
use strict; | |
use diagnostics; | |
my $phrase = '{Пожалуйста|Просто} сделайте так, чтобы это {удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно} случайным образом|менялось каждый раз}'; | |
my $regex; | |
my (@match); | |
my $idx = 0; | |
$regex = qr/\{((?:[^{}]+|(??{$regex}))*)\}(?{$match[$idx++]=$1;})/s; | |
sub shuffle { | |
my $phrase = shift || die 'No phrase given'; | |
$phrase =~ s/$regex/ | |
my $variant = $1; | |
# Надо проверить, есть ли "паттерны" внутри паттернов. Немного криво получилось ( | |
if($variant =~ m{\{[^\}]*\}}g) { | |
$variant = main::shuffle($variant); | |
} | |
my @variants = split '\|', $variant; | |
$variants[rand @variants]; | |
/ge; | |
return $phrase; | |
} | |
print main::shuffle($phrase),"\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment