Jump to content

Mad Libs: Difference between revisions

m
→‎{{header|REXX}}: removed some dead code, changed comments, indentation, added a section for the '''CHANGESTR""" bif. -- ~~~~
m (→‎{{header|REXX}}: typos (mostly))
m (→‎{{header|REXX}}: removed some dead code, changed comments, indentation, added a section for the '''CHANGESTR""" bif. -- ~~~~)
Line 590:
=={{header|REXX}}==
<lang rexx>/*REXX program to prompt user for template substitutions within a story.*/
@=; @.=; !.=0; #=0; old.@= /*assign some defaults. */
parse arg iFID . /*allow useruse to specify inputfileinput 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-FE─O─F or blank line.*/
 
do k=1 forever /*look for templates in the 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 /*Markmark 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 template counter . */
old.# = '<'?">"; new.# =ans ans /*assign "old" name, & "new" name. */
end /*kforever*/
 
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 substitutions in txt text. */
@.m = changestr(old.n, @.m, new.n)
end /*n*/
say @.m /*display (new) substituted text.*/
end /*m*/
 
say copies('═',79) /*display a final (output) 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
 
Line 639:
═══════════════════════════════════════════════════════════════════════════════
</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}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.