Run-length encoding: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: made incrementing of the DO loop index more idiomatic, changed whitespade and comments, simplified encoding. -- ~~~~)
m (→‎decoding: made the DO loop more idiomatic. -- ~~~~)
Line 2,747: Line 2,747:


===decoding===
===decoding===
<lang rexx>/*REXX program decodes string by using a run-length scheme (min len=2).*/
<lang rexx>/*REXX program decodes a string by using a run-length scheme. */
parse arg x /*normally, input would be a file*/
parse arg x . /*normally, input would be a file*/
if x=='' then x='11WB11W2B23WB13W' /*No input? Then use the default*/
if x=='' then x='11WB11W2B23WB13W' /*No input? Then use the default*/
Lx=length(x) /*get the length of the X string.*/
Lx=length(x) /*get the length of the X string.*/
y= /*Y is the output string (so far)*/
y= /*Y is the output string (so far)*/
do j=1 to Lx /*warning! J is modified below.*/
do j=1 by 0 to Lx /*warning! J is modified below.*/
c=substr(x,j,1)
c=substr(x,j,1)
if \datatype(c,'W') then do /*a loner char, simply add to OUT*/
if \datatype(c,'W') then do /*a loner char, simply add to OUT*/
y=y || c
y=y || c; j=j+1; iterate /*j*/
iterate
end
end
d=1
d=1
do k=j+1 to Lx while datatype(substr(x,k,1),'w') /*look for #end*/
do k=j+1 to Lx while datatype(substr(x,k,1),'w') /*look for #end*/
d=d+1 /*d is the number of digs so far.*/
d=d+1 /*D is the number of digs so far.*/
end /*k*/
end /*k*/


n=substr(x,j,d)+1 /*D is length of encoded number.*/
n=substr(x,j,d)+1 /*D is length of encoded number.*/
y=y || copies(substr(x,k,1),n) /*N is now the number of chars. */
y=y || copies(substr(x,k,1),n) /*N is now the number of chars. */
j=j+d /*A bad thing to do, but simple. */
j=j+1+d /*increment the DO loop index. */
end /*j*/
end /*j*/