Run-length encoding: Difference between revisions

m
→‎{{header|REXX}}: combined the version 1 encode and decode into one program, added/changed comments and whitespace.
m (added whitespace.)
m (→‎{{header|REXX}}: combined the version 1 encode and decode into one program, added/changed comments and whitespace.)
Line 4,137:
===version 1===
The task (input) rule was relaxed a bit as this program accepts upper─ and lowercase input.
 
An error message is generated if the input text is invalid.
 
Note that this REXX version (for encoding and decoding) uses a   ''replication''   count, not the   ''count''   of characters,
<br>so a replication count of &nbsp; '''11''' &nbsp; represents a count of &nbsp; '''12''' &nbsp; characters.
====encoding====
<lang rexx>/*REXX program encodes and displays a string by using a run─length encoding scheme. */
parse arg xinput . /*normally, input would be in a file. */
defdefault= 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW' /*default.*/
if xinput=='' | xinput=="," then xinput= def default /*Not specified? Then use the default.*/
$encode= RLE(input) ; say ' input=' input /*$:encode is the outputinput string; (sodisplay far)input. */
Lx= length(x) say 'encoded=' encode /* /*get the length of the X string. display run─len*/
do jdecode=1 by 0 to LxRLD(encode); say c'decoded=' substr(x, j, 1)decode /*J:decode the isrun─len; incremented within thedisplay loopdecode. */
sayexit 0 'decoded=' $ /*stick a fork in it, we're all done. */</lang>
if \datatype(c, 'M') then do; say "error!: data isn't alphabetic:" c; exit 13; end
/*──────────────────────────────────────────────────────────────────────────────────────*/
r= 0 /*R: is NOT the number of characters. */
err: say; if \datatype(c, 'M') then do; say "***error!:*** input data isn't alphabetic:" c; exit 13 say; end exit 13
do k=j+1 to Lx while substr(x, k, 1)==c /*while characters ≡ C. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
r= r + 1 /*bump the replication count for a char*/
RLE: procedure; parse arg x; $= end /*k$: is the output string (so far). */
j= j + r + 1 Lx= length(x) /*incrementget (addlength to)of the DOplain looptext indexstring. */
if r==0 then $do j= $ || 1 by 0 to Lx; c= substr(x, j, 1) /*don'tobtain usea character Rfrom plain iftext. it is equal to zero.*/
if else $= $ || r || \datatype(c, 'M') then call err /*addCharacter characternot toa theletter? encoded string.Issue error.*/
r= 0 r= 0 /*R: is NOT the number of characters. */
end /*j*/
do k=j+1 to Lx while substr(x, k, 1)==c /*while characters ≡ C. */
 
r= r + 1 /*bump the replication count for a char*/
say ' input=' x
say 'encoded=' $ end /*stick a fork in it, we're all done. k*/</lang>
j= j + #r + 1 /*increment (add to) the DO loop index by D+1. */
{{out|output|text=&nbsp; when using the default input:}}
if r==0 then $= $ || c /*don't use R if it is equal to zero.*/
<pre>
#= # + 1 else $= $ || r || c /*bump theadd countcharacter ofto the numericencoded charsstring. */
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
end /*kj*/; return $ /*#: return the number ofencoded charactersstring soto farcaller. */
output= 11WB11W2B23WB13W
/*──────────────────────────────────────────────────────────────────────────────────────*/
</pre>
RLD: procedure; parse arg x; $= /*$: is the output string (so far). */
 
n= substr(x, j, #) + 1 Lx= length(x) /*#:get isthe length of the encoded numberstring. */
====decoding====
do j=1 by 0 to Lx; c= substr(x, j, 1) /*obtain a character from run encoding.*/
<lang rexx>/*REXX program decodes and displays a string by using a run─length decoding scheme. */
parse arg x . if \datatype(c, 'W') then do; $= $ || c; j= j + 1; iterate /*normally, input would be in a file. j*/
if x=='' | x=="," then x= 11WB11W2B23WB13W end /*X [↑] not specified?a loner char, add Thenit useto defaultoutput.*/
$= #= 1 /*$: is the output string (so far). [↓] W: use a Whole number*/
do k=j+1 to Lx Lx=while lengthdatatype(substr(x,k,1), 'w') /*get the length of the input string. while numeric*/
do j #= # + 1 by 0 to Lx; c= substr(x, j, 1) /*Note: J is modified within loop /*bump the count of the numeric chars. */
if \datatype(c, 'W') then do end /*a loner char, simply add to output. k*/
n= substr(x, j, #) + 1 $= $ ||/*#: c; the length j=of jencoded +character. 1; iterate /*j*/
$= $ || copies( substr(x, k, 1), n) /*N: is now the number of characters. end*/
#= 1 j= j + # + 1 /*increment the DO loop [↓]index by D+1. W: use a Whole number.*/
doend k= /*j+1*/; to Lx while datatype( substr(x,k,1), 'w') return $ /*whilereturn athe partdecoded isstring numericto caller. */</lang>
#= # + 1 /*bump the count of the numeric chars. */
end /*k*/ /*#: the number of characters so far. */
 
n= substr(x, j, #) + 1 /*#: is length of the encoded number. */
$= $ || copies( substr(x, k, 1), n) /*N: is now the number of characters. */
j= j + # + 1 /*increment the DO loop index by D+1. */
end /*j*/
 
say ' input=' x
say 'decoded=' $ /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
input= 11WB11W2B23WB13W
outputencoded= 11WB11W2B23WB13W
decoded= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>