Word wrap: Difference between revisions

Content added Content deleted
(→‎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: Line 1,558:
</pre>
</pre>
('''L'''eft &nbsp; is the default.)
('''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.*/
parse arg iFID width justify _ . /*get optional CL args.*/
if iFID='' |iFID==',' then iFID ='LAWS.TXT' /*default input file ID*/
if iFID='' |iFID==',' then iFID ='LAWS.TXT' /*default input file ID*/
Line 1,573: Line 1,576:
if _\=='' then call err "too many arguments specified." _
if _\=='' then call err "too many arguments specified." _
if \datatype(width,'W') then call err "WIDTH (2nd arg) isn't an integer:" width
if \datatype(width,'W') then call err "WIDTH (2nd arg) isn't an integer:" width
@= /*nullify the text (so far). */
n=0 /*number of words in the file. */
do j=0 while lines(iFID)\==0 /*read from the file until E-O-F.*/
do j=0 while lines(iFID)\==0 /*read from the file until E-O-F.*/
@=@ linein(iFID) /*append the file's text to @ */
_=linein(iFID) /*get a record (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 j==0 then call err 'file' iFID "not found."
if @='' then call err 'file' iFID "is empty."
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 k=2 for words(@)-1; x=word(@,k) /*parse until text (@) exhausted.*/
do m=2 for n-1; x=@.m /*parse until text (@) exhausted.*/
_=$ x /*append it to the money and see.*/
_=$ x /*append it to the money and see.*/
if length(_)>width then call tell /*word(s) exceeded the width? */
if length(_)>width then call tell /*this word a bridge too far? >w*/
$=_ /*the new words are OK so far. */
$=_ /*the new words are OK so far. */
end /*k*/
end /*m*/

call tell /*handle any residual words. */
call tell /*handle any residual words. */
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
Line 1,593: Line 1,598:
/*──────────────────────────────────TELL subroutine─────────────────────*/
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: if $=='' then return /*first word may be too long. */
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.*/
select
else do
when just=='B' then $=justify($,w) /*◄────both────►*/
w=max(width,length($)) /*don't truncate long words.*/
when just=='C' then $= center($,w) /* ◄centered► */
select
when just=='L' then $= strip($) /*left ◄────────*/
when just=='R' then $= right($,w) /*──────► right */
when just=='R' then $= right($,w) /*──────► right */
when just=='B' then $=justify($,w) /*◄────both────►*/
end /*select*/
when just=='C' then $= center($,w) /* ◄centered► */
end /*select*/
end
say $ /*show and tell, or write──►file?*/
say $ /*show and tell, or write──►file?*/
_=x /*handle any word overflow. */
_=x /*handle any word overflow. */