Anonymous user
Mad Libs: Difference between revisions
→{{header|Pike}}: fix code to match intent as gleaned from wp:Mad Libs
(→{{header|Pike}}: fix code to match intent as gleaned from wp:Mad Libs) |
|||
Line 62:
=={{header|Pike}}==
this solution uses readline to make editing more convenient
<lang Pike>#!/usr/bin/pike
Line 73:
Names or objects in the story can be made variable by
referencing them as <person> <object>, etc.
End the story with an empty line.▼
Type show to read the story. You will be asked to fill the variables,
and the the story will be shown.
▲End the story with an empty line.
Type help to see this message again.
Type exit to quit.
");
foreach(variables; string name; string value)▼
}
void add_line(string input)
{
array variables = parse_for_variables(input);
story += input+"\n";
}
{
array vars = Array.flatten(array_sscanf(input, "%*[^<>]%{<%[^<>]>%*[^<>]%}%*[^<>]"));
▲ write("Found new variable \"%s\"\n", name);
}
{
array
{
▲ string new_value = readln->read(sprintf("Please give a name for %s: ", name));
}
return
}
void show_story(string story)
{
write("\n"+replace(story, variables));
}
Line 159 ⟶ 121:
}
▲mapping variables = ([]);
mapping functions = ([ "help":print_help,
"show":show_story,
Line 180 ⟶ 141:
exit(0);
if(input == "")
show_story(story);
else if (functions[input])
functions[input]();
else
}
}</lang>
Line 193 ⟶ 153:
Names or objects in the story can be made variable by
referencing them as <person> <object>, etc.
Type show to read the story. You will be asked to fill the variables,
and the the story will be shown.
▲ End the story with an empty line.
Type help to see this message again.
Type exit to quit.
> <person> is a programmer.
Found
> <he or she> created <website> for all of us to enjoy.
Found
>
Please
Please
Please
Michael is a programmer.
he created RosettaCode for all of us to enjoy.
>
Please name a person: Guilaumme
Please name a he or she: he
Please name a website: PLEAC
Guilaumme is a programmer.
he created PLEAC for all of us to enjoy.
> exit
|