Mad Libs: Difference between revisions
Content added Content deleted
Walterpachl (talk | contribs) 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; @= /*assign some defaults. */ |
|||
parse arg iFID . /*allow |
parse arg iFID . /*allow use to specify input file*/ |
||
if iFID=='' then iFID="MAD_LIBS.TXT" |
if iFID=='' then iFID="MAD_LIBS.TXT" /*Not specified? Then use default*/ |
||
do recs=1 while lines(iFID)\==0 |
do recs=1 while lines(iFID)\==0 /*read the input file 'til done. */ |
||
@.recs=linein(iFID); @=@ @.recs |
@.recs=linein(iFID); @=@ @.recs /*read a record, append it to @ */ |
||
if @.recs='' then leave |
if @.recs='' then leave /*Read a blank line? We're done.*/ |
||
end /*recs*/ |
end /*recs*/ |
||
recs=recs-1 /*adjust for |
recs=recs-1 /*adjust for E─O─F or blank line.*/ |
||
do |
do forever /*look for templates in the text.*/ |
||
parse var @ '<' ? '>' |
parse var @ '<' ? '>' @ /*scan for <ααα> stuff in text.*/ |
||
if ?='' then leave /*if no |
if ?='' then leave /*if no ααα, then we're done. */ |
||
if !.? then iterate /*already asked? Keep scanning.*/ |
if !.? then iterate /*already asked? Keep scanning.*/ |
||
!.?=1 /* |
!.?=1 /*mark this ααα 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 |
#=#+1 /*bump the template counter. */ |
||
old.#='<'?">"; new.#= |
old.# = '<'?">"; new.# = ans /*assign "old" name & "new" name.*/ |
||
end /* |
end /*forever*/ |
||
say; say copies('═',79) |
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 |
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 |
|||
Mary |
Mary |
||
─────────── please enter a word or phrase to replace: he or she |
|||
she |
she |
||
─────────── 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}}== |