Skip to content

Instantly share code, notes, and snippets.

@vzarytovskii
Created November 18, 2011 09:20
Show Gist options
  • Save vzarytovskii/1375984 to your computer and use it in GitHub Desktop.
Save vzarytovskii/1375984 to your computer and use it in GitHub Desktop.
Randomize phrase
#!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