LZW compression: Difference between revisions

→‎{{header|REXX}}: added a 2nd REXX version.
m (→‎{{header|Sidef}}: modified code to work with the latest version of Sidef)
(→‎{{header|REXX}}: added a 2nd REXX version.)
Line 3,477:
</pre>
 
=={{header|REXX}}==
===version 1===
{{trans|Java}}
<lang rexx>/* REXX ---------------------------------------------------------------
Line 3,566 ⟶ 3,567:
dec=To be or not to be that is the question!
decompression ok
</pre>
 
===version 2===
{{trans|Java}}
This REXX version can execute with '''ASCII''' or '''EBCDIC''' systems.
<lang rexx>/*REXX pgm compresses text using LZW (Lempel-Ziv-Welch), and reconstitutes it.*/
parse arg x /*get an optional argument from the CL.*/
if x=='' then x='"There is nothing permanent except change." ─── Heraclitus [540-475 BC]'
say 'original text=' x
cypher=LZWc(x) /*compress text using the LZW algorithm*/
say 'reconstituted=' LZWd(cypher)
say ' LZW integers=' cypher /*also display the LZW integers used. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
LZWc: procedure; parse arg y,,w $ @.; #=256 /*the LZW compress algorithm.*/
do j=0 for 256; _=d2c(j); @._=j; end /*j*/
 
do k=1 for length(y); _=w || substr(y,k,1)
if @._=='' then do; $=$ @.w
@._=#; #=#+1
w=substr(y,k,1)
end
else w=_
end /*k*/
return strip($ @.w) /*remove any superfluous blanks*/
/*────────────────────────────────────────────────────────────────────────────*/
LZWd: procedure; parse arg w y,,@.; #=256 /*the LZW decompress algorithm.*/
do j=0 for 256; @.j=d2c(j); end /*j*/
$=@.w; w=$
do k=1 for words(y); _=word(y,k)
if @._\=='' | @.k==' ' then ?=@._
else if _=# then ?=w || left(w,1)
$=$ || ?
@.#=w || left(?,1); #=#+1
w=?
end /*k*/
return $</lang>
'''output''' &nbsp; using the default input:
<pre>
original text= "There is nothing permanent except change." ─── Heraclitus [540-475 BC]
reconstituted= "There is nothing permanent except change." ─── Heraclitus [540-475 BC]
LZW integers= 34 84 104 101 114 101 32 105 115 32 110 111 116 104 105 110 103 32 112 259 109 97 110 101 110 116 32 101 120 99 101 112 281 99 104 277 103 101 46 34 32 196 297 32 72 259 97 99 108 105 116 117 264 91 53 52 48 45 52 55 53 32 66 67 93
</pre>