首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Lua中的LZW压缩

Lua中的LZW压缩
EN

Stack Overflow用户
提问于 2012-08-01 00:15:37
回答 2查看 2.6K关注 0票数 1

这是Lempel-Ziv-Welch压缩的伪代码。

代码语言:javascript
复制
 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行收到了“尝试调用字符串值”错误。

代码语言:javascript
复制
 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压缩。非常感谢!

EN

Stack Overflow用户

发布于 2012-08-01 00:48:10

你也应该看看here下载一个高性能的LZW压缩算法在Lua的源代码。

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11744573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档