Anonymous user
Mad Libs: Difference between revisions
Rewritten D entry, fits better the Task
(Simpler regex in D entry) |
(Rewritten D entry, fits better the Task) |
||
Line 212:
=={{header|D}}==
{{trans|
<lang d>import std.stdio, std.regex, std.
std.string, std.range;▼
void main() {
while (true) {
▲ writeln("The story template is:\n", story);
if (line.empty) break;
}
auto re = regex("<.+?>", "g");
auto fields = story.match(re).map!q{a.hit}().array().sort().uniq();
writef("Enter a value for '%s': ", field[1 .. $ - 1]);
▲ const values = std.string.split(stdin.readln().strip(), ",");
story = story.replace(field, stdin.readln().strip());
}
▲ story = story.replace(f, v);
writeln("\nThe story becomes:\n", story);
}</lang>
{{out}}
<pre>
<name> went for a walk in the park. <he or she>
found a <noun>. <name> decided to take it home.
Enter a value for 'he or she': She
Enter a
Enter a value for 'noun': cockerel
The story becomes:
|