Jump to content

Longest string challenge: Difference between revisions

→‎read file until not ready: re-worked REXX program to NOT use a variable to hold the length of a record or the maximum record, elided the use of an IF statement..
(Delete dead link to non-rosetta URI and referencing sentence.)
(→‎read file until not ready: re-worked REXX program to NOT use a variable to hold the length of a record or the maximum record, elided the use of an IF statement..)
Line 1,775:
In the REXX language,   ''everything''   is a string (characters).
===read file until not ready===
This REXX version adheres to spirit (and letter) of all the restrictions for this task:
No comparators are used   (such as   '''if'''   or   '''when'''),
::*   no comparators are used,   including those within:
<br>no output is produced when the file is empty &nbsp; (or contains all null strings),
No comparators are used::::* &nbsp; (such as &nbsp;&nbsp; '''if''' &nbsp; or&nbsp; &nbsp; '''when''' (statements),
<br>no arrays or lists are used, &nbsp; and
::::* &nbsp; '''when''' &nbsp; (statements)
<br>no additions or subtractions are used.
::::* &nbsp; '''until''' &nbsp;&nbsp; (clauses) &nbsp; and
<lang rexx>/*REXX program reads a file and displays the longest [widest] record(s) [line(s)].*/
parse::::* arg&nbsp; iFID . '''while''' &nbsp;&nbsp;(clauses) /*obtain optional argument from the CL.*/
<br>::* &nbsp; no output is produced when the file is empty &nbsp; (or contains all null strings),
if iFID=='' | iFID=="," then iFID= 'LONGEST.TXT' /*Not specified? Then use the default.*/
<br>::* &nbsp; no arrays or lists are used, &nbsp; and
<br>::* &nbsp; no additions or subtractions are used., &nbsp; and
::* &nbsp; no variables are used to hold the length of (any) record.
<lang rexx>/*REXX program reads a file and displays the longest [widest] record(s) [line(s)]. */
signal on notReady /*when E-O-F is reached, jump/branch. */
iFID= 'LONGEST.TXT' /*the default file identifier for input*/
 
parse arg fid . /*obtain optional argument from the CL.*/
do r=1; _=linein(iFID); w=length(_) /*read a line from the input file. */
do y#=r 1 to 1length(fid); miFID=wfid; $=y ||leave _; iterate/*if r;specified, then enduse what's given. /*first initialization.*/
end /*r#*/
do n=m for 1; end /*for smaller records. */
!= do z=n to w for 1; $=; end /*foundthe longestmaximum recordwidth (so far). */
do forever; _=linein(iFID); do x?=m_ to w for 1; $=$'a0d'x ||/*read _;a line from the input file. end /*append record to $. */
mt=max(m,0 w) /*M: is the maximum length record. /*don't do the initialization next time*/
do n#=mt for 1t; !=?; ?=; $=. || _; end /*forjust smallerdo records.1st time.*/
end /*r*/
do #=length(!' ') to length(?) for 1; $=; end /*found widest rec.*/
 
notReady: do j=m for m do #=length(!) to length(?) for 1; $=$'a0d'x || _; end /*handleappend theit caseto of no input$. */
say substr($, 2) /*display (all)[↑] the longestvariable records. # isn't really used.*/
!=left(., max( length(!), length(?) ) exit ) /*stick!: a forkis inthe it,max length we'rerecord, allso donefar. */
end /*jforever*/</lang>
/* [↓] comes here when file gets E─O─F*/
notReady: do r=1; do _j=lineinlength(iFID!); w=for length(_!) /*readhandle athe linecase fromof theno input file. */
say substr($, 2) /*display (all) the longest records. */
exit /*stick a fork in it, we're all done. */
end /*j*/</lang>
{{out|input|text=file &nbsp; '''LONGEST.TXT''':}}
<pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.