Just in time processing on a character stream: Difference between revisions
Content added Content deleted
(→Tcl: Added implementation) |
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
||
Line 11: | Line 11: | ||
This task was inspired by the movie "[[wp:National_Treasure_%28film%29|National Treasure]]" with refers to a "[[wp:Book cipher|book cipher]]". |
This task was inspired by the movie "[[wp:National_Treasure_%28film%29|National Treasure]]" with refers to a "[[wp:Book cipher|book cipher]]". |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX pgm extracts characters by using a book cipher from a text file. */ |
|||
parse arg iFID /*optional name of file (book). */ |
|||
if iFID=='' then iFID="JIT.TXT" /*Not specified? Then use default*/ |
|||
errTxt= ' is missing or not a positive integer: ' /*error text string.*/ |
|||
$='abcdefghijklmnopqrstuvwxyz'; _=$; upper _; $= '0123456789'$ || _ |
|||
ask='──────────enter four parameters (all positive integers) or QUIT' |
|||
pag=1; lin=1; @.= /*assume start of page 1, line 1.*/ |
|||
/*read the entire book from file.*/ |
|||
do while lines(iFID)\==0 /*process lines from input stream*/ |
|||
_=translate(linein(iFID),,'9'x) /*obtain a single line from input*/ |
|||
if pos('c'x,_)\==0 then do /*was a ff (form-feed) detected? */ |
|||
pag=pag+1 /*bump page counter for next page*/ |
|||
lin=1 /*reset the line counter to one. */ |
|||
end /* [↑] handle finding of formfeed*/ |
|||
@.pag.lin=_ /*obtain a single line from input*/ |
|||
lin=lin+1 /*bump the line counter. */ |
|||
end /*while lines···*/ /* [↑] read entire input stream.*/ |
|||
?= /*define the phrase to be null.*/ |
|||
do prompt=0; bad=1 /*get J.I.T. positional numbers. */ |
|||
say ask; pull y /*obtain the numbers from console*/ |
|||
if y='QUIT' then exit 0 /*the user wants out of this loop*/ |
|||
y=space(translate(y,$,$||xrange())) /*allow any sep user wants.*/ |
|||
parse var y a.1 a.2 a.3 a.4 /*parse the pertinent parameters.*/ |
|||
if a.1=='QUIT' then exit 0 /*the user wants out of this loop*/ |
|||
if words(y)>4 then do /*did the user have fingeritis ? */ |
|||
say 'too many parameters entered.' |
|||
iterate prompt |
|||
end /*go and try again to get parms. */ |
|||
do k=1 for 4 /*validate parms {positive ints}.*/ |
|||
if datatype(a.k,'W') & a.k>0 then iterate /*found a bad parm.*/ |
|||
say 'parameter ' k errTxt a.k /*issue an error message to term.*/ |
|||
iterate prompt /*go & ask for another set of #s.*/ |
|||
a.k=a.k/1 /*normalize the user's number. */ |
|||
end /*k*/ /* [↑] done with the validations*/ |
|||
parse var y p l w c /*parse parameters for names. */ |
|||
x=substr(word(@.p.l,w),c,1) /*extract the character from book*/ |
|||
if x=='!' then leave /*if the stop char found, done.*/ |
|||
say right(x '◄─── a letter',46) /*might as well echo char to term*/ |
|||
?=? || x /*append the character to phrase.*/ |
|||
end /*j*/ /* [↑] go & keep building phrase*/ |
|||
say '═════►' ? /*display letters found from book*/ |
|||
/*stick a fork in it, we're done.*/</lang> |
|||
'''input''' supplied to the console (terminal) by the user (the commas are optional): |
|||
<pre> |
|||
1, 133, 4, 5 |
|||
1, 34, 9, 3 |
|||
1, 1377, 2, 2 |
|||
1, 4, 8, 4 |
|||
1, 265, 3, 5 |
|||
1, 413, 10, 2 |
|||
1, 10, 12, 1 |
|||
1, 144, 10, 10 |
|||
1, 571, 4, 12 |
|||
</pre> |
|||
'''output''' (abbridged) |
|||
<pre> |
|||
──────────enter four parameters (all positive integers) or QUIT |
|||
1, 133, 4, 5 |
|||
s ◄─── a letter |
|||
──────────enter four parameters (all positive integers) or QUIT |
|||
· |
|||
· |
|||
· |
|||
═════► so─true. |
|||
</pre> |
|||
The input file used (the IBM jargon file) in the above REXX program can be found here. |
|||
<br> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |