Run-length encoding: Difference between revisions

Content deleted Content added
Jono (talk | contribs)
→‎{{header|Lasso}}: Adding Lasso RLE Example
→‎{{header|REXX}}: made incrementing of the DO loop index more idiomatic, changed whitespade and comments, simplified encoding. -- ~~~~
Line 2,718: Line 2,718:
The task (input) rule was relaxed a bit as this program accepts upper- and lowercase input.
The task (input) rule was relaxed a bit as this program accepts upper- and lowercase input.
===encoding===
===encoding===
<lang rexx>/*REXX program encodes string by using a run-length scheme (min len=2).*/
<lang rexx>/*REXX program encodes 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*/
/*═══ arg x . ═══*/ /*◄── use if X must be uppercase.*/
def='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
def= 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
if x=='' then x=def /*No input? Then use the default*/
if x='' then x=def /*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 /*J is incremented (below). */
c=substr(x,j,1) /*pick a character, check for err*/
c=substr(x,j,1) /*pick a character, check for err*/
if \datatype(c,'m') then do;say "error!: data isn't alphabetic";exit 13; end
if \datatype(c,'M') then do; say "error!: data isn't alphabetic:" c; exit 13; end
r=0 /*R is NOT the number of chars. */
r=0 /*R is NOT the number of chars. */
do k=j+1 to Lx while substr(x,k,1)==c

r=r+1 /*R is a replication count. */
do k=j+1 to Lx while substr(x,k,1)==c
r=r+1 /*R is a replication count. */
end /*k*/
j=j+1+r /*modify (add to) the do index. */
end /*k*/
if r==0 then r= /*don't use R if R is zero.*/

if r==0 then Y = Y || c /*C wan't repeated, just OUT it.*/
Y = Y || r || c /*add it to the encoded string.*/
else Y = Y || r || c /*add it to the encoded string. */
j=j+r /*A bad thing to do, but simple. */
end /*j*/
end /*j*/