Mad Libs: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: typos (mostly))
m (→‎{{header|REXX}}: removed some dead code, changed comments, indentation, added a section for the '''CHANGESTR""" bif. -- ~~~~)
Line 590: Line 590:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to prompt user for template substitutions within a story.*/
<lang rexx>/*REXX program to prompt user for template substitutions within a story.*/
@=; @.=; !.=0; #=0; old.= /*assign some defaults. */
@.=; !.=0; #=0; @= /*assign some defaults. */
parse arg iFID . /*allow user to specify inputfile*/
parse arg iFID . /*allow use to specify input file*/
if iFID=='' then iFID="MAD_LIBS.TXT" /*Not specified? Then use default*/
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. */
do recs=1 while lines(iFID)\==0 /*read the input file 'til done. */
@.recs=linein(iFID); @=@ @.recs /*read a record, append it to @ */
@.recs=linein(iFID); @=@ @.recs /*read a record, append it to @ */
if @.recs='' then leave /*Read a blank line? We're done.*/
if @.recs='' then leave /*Read a blank line? We're done.*/
end /*recs*/
end /*recs*/


recs=recs-1 /*adjust for E-O-F or blank line.*/
recs=recs-1 /*adjust for E─O─F or blank line.*/


do k=1 /*look for templates in text. */
do forever /*look for templates in the text.*/
parse var @ '<' ? '>' @ /*scan for <xxx> stuff in text.*/
parse var @ '<' ? '>' @ /*scan for <ααα> stuff in text.*/
if ?='' then leave /*if no XXX, then we're done. */
if ?='' then leave /*if no ααα, then we're done. */
if !.? then iterate /*already asked? Keep scanning.*/
if !.? then iterate /*already asked? Keep scanning.*/
!.?=1 /*Mark this aName as "found". */
!.?=1 /*mark this ααα as "found". */
do forever /*prompt user for a replacement. */
do forever /*prompt user for a replacement. */
say '────────── please enter a word or phrase to replace: ' ?
say '─────────── please enter a word or phrase to replace: ' ?
parse pull ans; if ans\='' then leave
parse pull ans; if ans\='' then leave
end /*forever*/
end /*forever*/
#=#+1 /*bump the template counter */
#=#+1 /*bump the template counter. */
old.#='<'?">"; new.#=ans /*assign "old" name, "new" name. */
old.# = '<'?">"; new.# = ans /*assign "old" name & "new" name.*/
end /*k*/
end /*forever*/


say; say copies('═',79) /*display a blank and a fence. */
say; say copies('═',79) /*display a blank and a fence. */


do m=1 for recs /*display the text, line for line*/
do m=1 for recs /*display the text, line for line*/
do n=1 for # /*perform substitutions in txt */
do n=1 for # /*perform substitutions in text. */
@.m=changestr(old.n, @.m, new.n)
@.m = changestr(old.n, @.m, new.n)
end /*n*/
end /*n*/
say @.m /*display (new) substituted text.*/
say @.m /*display (new) substituted text.*/
end /*m*/
end /*m*/


say copies('═',79) /*display a final fence. */
say copies('═',79) /*display a final (output) fence.*/
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
'''output''' when using the default input fileID
'''output''' when using the default input fileID
<pre style="overflow:scroll">
<pre style="overflow:scroll">
────────── please enter a word or phrase to replace: name
─────────── please enter a word or phrase to replace: name
Mary
Mary
────────── please enter a word or phrase to replace: he or she
─────────── please enter a word or phrase to replace: he or she
she
she
────────── please enter a word or phrase to replace: noun
─────────── please enter a word or phrase to replace: noun
little lamb
little lamb


Line 639: Line 639:
═══════════════════════════════════════════════════════════════════════════════
═══════════════════════════════════════════════════════════════════════════════
</pre>
</pre>
Some older REXXes don't have the '''changestr''' bif, so here is a version:
<lang rexx>/*──────────────────────────────────CHANGESTR subroutine────────────────*/
changestr: procedure; parse arg o,h,n; r=; w=length(o); if w==0 then return n||h
do forever; parse var h y (o) _ +(w) h; if _=='' then return r||y; r=r||y||n; end</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==