這是Lempel-Ziv-Welch壓縮的僞代碼。LZW在Lua中的壓縮
pattern = get input character
while (not end-of-file) {
K = get input character
if (<<pattern, K>> is NOT in
the string table){
output the code for pattern
add <<pattern, K>> to the string table
pattern = K
}
else { pattern = <<pattern, K>> }
}
output the code for pattern
output EOF_CODE
我想在Lua中對此進行編碼,但實際上並不奏效。下面是我在Python的LZW功能仿照代碼,但我得到一個「試圖調用一個字符串值」錯誤在線8.
function compress(uncompressed)
local dict_size = 256
local dictionary = {}
w = ""
result = {}
for c in uncompressed do
-- while c is in the function compress
local wc = w + c
if dictionary[wc] == true then
w = wc
else
dictionary[w] = ""
-- Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size = dict_size + 1
w = c
end
-- Output the code for w.
if w then
dictionary[w] = ""
end
end
return dictionary
end
compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
print (compressed)
我真的很喜歡一些幫助或者讓我的代碼運行,或者幫助我在Lua中編碼LZW壓縮。非常感謝!
可能重複:[LZW數據壓縮在Lua中(http://stackoverflow.com/ q/11745265) – 2012-07-31 17:33:14
[LZW數據壓縮在Lua]中可能的重複(http://stackoverflow.com/questions/11745265/lzw-data-compression-in-lua) – JosephH 2012-07-31 19:19:56