可能的重复:
Lua 中的 LZW 压缩
这是我在 Lua 中使用 LZW 压缩方法压缩数据的代码。我的问题是该函数返回字符“T”,而不是返回完整的压缩字符串“TOBEORNOTTOBEORNOT”。谢谢!
function compress(uncompressed)
local dict_size = 256
local dictionary = {}
w = ""
result = {}
for i = 1, #uncompressed do
local c = string.sub(uncompressed, i, i)
local wc = w .. c
if dictionary[wc] == true then
w = wc
else
dictionary[w] = ""
dictionary[wc] = dict_size
dict_size = dict_size + 1
w = c
end
if w then
dictionary[w] = ""
end
return w
end
end
compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
print(compressed)