Mad Libs: Difference between revisions

→‎{{header|Lua}}: added Lua solution
(Applesoft BASIC)
(→‎{{header|Lua}}: added Lua solution)
Line 1,803:
wait
</lang>
 
=={{header|Lua}}==
<lang lua>print("Enter a multi-line story (finish with blank line):")
dict, story, line = {}, "", io.read()
while #line>0 do story=story..line.."\n" line=io.read() end
story = story:gsub("(%<.-%>)", function(what)
if dict[what] then return dict[what] end
io.write("Please enter a " .. what .. ": ")
dict[what] = io.read()
return dict[what]
end)
print("\n"..story)</lang>
{{out}}
<pre>Enter a multi-line story (finish with blank line):
<name> went for a walk in the park. <he or she>
found a <noun>. <name> decided to take it home.
 
Please enter a <name>: Mark
Please enter a <he or she>: He
Please enter a <noun>: fork
 
Mark went for a walk in the park. He
found a fork. Mark decided to take it home.</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Anonymous user