Word wrap: Difference between revisions
Content added Content deleted
Walterpachl (talk | contribs) m (→{{header|Python}}=: fix tag) |
(→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 is the default.) |
('''L'''eft is the default.) |
||
<br><br>This version was modified (for speed at the expense of simplicity) to accommodate faster processing of large files. |
|||
⚫ | |||
<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. |
|||
⚫ | |||
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 |
||
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) /*get a record (line of text). */ |
|||
do words(_) /*extract some words (maybe not).*/ |
|||
⚫ | |||
n=n+1; parse var _ @.n _ /*get & assign next word in text.*/ |
|||
end /*DO words(_)*/ |
|||
⚫ | |||
call time 'R' |
|||
if j==0 then call err 'file' iFID "not found." |
if j==0 then call err 'file' iFID "not found." |
||
if |
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 |
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 |
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 /* |
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.*/ |
|||
else do |
|||
w=max(width,length($)) /*don't truncate long words.*/ |
|||
select |
|||
when just==' |
when just=='R' then $= right($,w) /*──────► right */ |
||
when just==' |
when just=='B' then $=justify($,w) /*◄────both────►*/ |
||
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. */ |