LZW compression: Difference between revisions

Content added Content deleted
Line 3,376: Line 3,376:


=={{header|Lua}}==
=={{header|Lua}}==

{{incorrect|Lua|Requires Lua 5.1+ based on syntax, yet none of Lua 5.1 - 5.4 will produce correct output: value of dec from decompress() is nil, second line of printed output is "nil". Result for this specific test case can be corrected by altering initial value of dictSize to 256 (from 255 in compress; from 0 in decompress), though it is unclear/untested if remainder of the dictionary maintenance code would be entirely functional as-is for the general case.}}


<lang lua>local function compress(uncompressed) -- string
<lang lua>local function compress(uncompressed) -- string
Line 3,402: Line 3,400:


local function decompress(compressed) -- table
local function decompress(compressed) -- table
local dictionary, dictSize, entry, result, w, k = {}, 0, "", {}, ""
local dictionary, dictSize, entry, w, k = {}, 256, "", string.char(compressed[1])
local result = {w}
for i = 0, 255 do
for i = 0, 255 do
dictionary[i] = string.char(i)
dictionary[i] = string.char(i)
end
end
for i = 1, #compressed do
for i = 2, #compressed do
k = compressed[i]
k = compressed[i]
if dictionary[k] then
if dictionary[k] then