Jump to content

Mad Libs: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(Rewritten D entry, fits better the Task)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 587:
Monica L. went for a walk in the park. She
found a cockerel. Monica L. decided to take it home.</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX program to prompt user for template subsitutions within a story. */
@=; @.=; !.=0; #=0; old.= /*assign some defaults. */
parse arg iFID . /*allow use to specify input file*/
if iFID=='' then iFID="MAD_LIBS.TXT" /*Not specified? Then use default*/
 
do recs=1 while lines(iFID)\==0 /*read the input file 'til done. */
@.recs=linein(iFID); @=@ @.recs /*read a record, append it to @ */
if @.recs='' then leave /*Read a blank line? We're done.*/
end /*recs*/
 
recs=recs-1 /*adjust for E-O-F or blank line.*/
 
do k=1 /*look for subsitutions in text. */
parse var @ '<' ? '>' @ /*scan for <xxx> stuff in text.*/
if ?='' then leave /*if no XXX, then we're done. */
if !.? then iterate /*already asked? Keep scanning.*/
!.?=1 /*Mark this aName as "found". */
do forever /*prompt user for a replacement. */
say '────────── please enter a word or phrase to replace: ' ?
parse pull ans; if ans\='' then leave
end /*forever*/
#=#+1 /*bump the subsitute name counter*/
old.#='<'?">"; new.#=ans /*assign "old" name, "new" name. */
end /*k*/
 
say; say copies('═',79) /*display a blank and a fence. */
 
do m=1 for recs /*display the text, line for line*/
do n=1 for # /*perform the subsitutions in txt*/
@.m=changestr(old.n, @.m, new.n)
end /*n*/
say @.m /*display (new) subsituted text. */
end /*m*/
 
say copies('═',79) /*display a final fence. */
/*stick a fork in it, we're done.*/</lang>
'''output''' when using the default input fileID
<pre style="overflow:scroll">
────────── please enter a word or phrase to replace: name
Mary
────────── please enter a word or phrase to replace: he or she
she
────────── please enter a word or phrase to replace: noun
little lamb
 
═══════════════════════════════════════════════════════════════════════════════
Mary went for a walk in the park. she
found a little lamb. Mary decided to take it home.
═══════════════════════════════════════════════════════════════════════════════
</pre>
 
=={{header|Ruby}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.