Jump to content

Word wrap: Difference between revisions

→‎version 1: used a stemmed array instead of a long character string, faster, but more complex and harder to follow than version 0. -- ~~~~
(→‎version 1: used a stemmed array instead of a long character string, faster, but more complex and harder to follow than version 0. -- ~~~~)
Line 1,558:
</pre>
('''L'''eft &nbsp; is the default.)
<br><br>This version was modified (for speed at the expense of simplicity) to accommodate faster processing of large files.
<lang rexx>/*REXX pgm reads a file and displays it (with word wrap to the screen). */
<br>Instead of appending lines of a file to a character string, the words are picked off and stored in a stemmed array.
<br>This decreases the amount of work that REXX has to do to retrieve (get) the next word in the (possibly) ginormous string.
<lang rexx>/*REXX pgm reads a file and displays it (with word wrap to the screen). */
parse arg iFID width justify _ . /*get optional CL args.*/
if iFID='' |iFID==',' then iFID ='LAWS.TXT' /*default input file ID*/
Line 1,573 ⟶ 1,576:
if _\=='' then call err "too many arguments specified." _
if \datatype(width,'W') then call err "WIDTH (2nd arg) isn't an integer:" width
@n= 0 /*nullifynumber theof textwords in (sothe far)file. */
do j=0 while lines(iFID)\==0 /*read from the file until E-O-F.*/
@_=@ linein(iFID) /*appendget thea file'srecord text to(line of @ text). */
do words(_) /*extract some words (maybe not).*/
end /*j*/
n=n+1; parse var _ @.n _ /*get & assign next word in text.*/
 
end /*DO words(_)*/
end /*j*/
call time 'R'
if j==0 then call err 'file' iFID "not found."
if @n=''=0 then call err 'file' iFID "is empty. (or has no words)"
$=@.1 /*init da money bag with 1st word*/
$=word(@,1)
do km=2 for words(@)n-1; x=word(@,k).m /*parse until text (@) exhausted.*/
_=$ x /*append it to the money and see.*/
if length(_)>width then call tell /*this word(s) exceededa the width?bridge too far? >w*/
$=_ /*the new words are OK so far. */
end /*km*/
 
call tell /*handle any residual words. */
exit /*stick a fork in it, we're done.*/
Line 1,593 ⟶ 1,598:
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: if $=='' then return /*first word may be too long. */
if just=='L' then $= strip($) /*left ◄────────*/
w=max(width,length($)) /*don't truncate very long words.*/
else selectdo
when just=='B' then $w=justifymax(width,length($,w)) /*don't truncate /*◄────both────►long words.*/
when just=='C' then $= center($,w) /* ◄centered► */select
when just=='LR' then $= stripright($,w) /*left──────► right ◄────────*/
when just=='RB' then $= rightjustify($,w) /*──────► right ◄────both────►*/
end when just=='C' then $= center($,w) /*select ◄centered► */
end /*select*/
end
say $ /*show and tell, or write──►file?*/
_=x /*handle any word overflow. */
Cookies help us deliver our services. By using our services, you agree to our use of cookies.