Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8316279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:19:35+00:00 2026-06-08T21:19:35+00:00

Here is the Pseudocode for Lempel-Ziv-Welch Compression. pattern = get input character while (

  • 0

Here is the Pseudocode for Lempel-Ziv-Welch Compression.

 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

I am trying to code this in Lua, but it is not really working. Here is the code I modeled after an LZW function in Python, but I am getting an “attempt to call a string value” error on line 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)

I would really like some help either getting my code to run, or helping me code the LZW compression in Lua. Thank you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:19:36+00:00Added an answer on June 8, 2026 at 9:19 pm

    Assuming uncompressed is a string, you’ll need to use something like this to iterate over it:

    for i = 1, #uncompressed do
      local c = string.sub(uncompressed, i, i)
      -- etc
    end   
    

    There’s another issue on line 10; .. is used for string concatenation in Lua, so this line should be local wc = w .. c.

    You may also want to read this with regard to the performance of string concatenation. Long story short, it’s often more efficient to keep each element in a table and return it with table.concat().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is pseudocode for what I am hoping to do: Open text File Find
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
What am i doing wrong here? Java code for computing prefix function. Two input
I'm concerned that I might be using a code pattern that leaks memory. Here's
here is a pseudoCode 1. View myView = new View(); 2. Animation myAnimation =
Here's some pseudocode for what I want my makefile to do: if (A doesn't
What language should I use for file and string manipulation? This might seem objective,
Hi i need to generate 9 digit unique account numbers. Here is my pseudocode:
I am trying to combine these MySQL queries but not getting them right. Here
I have looked around for a way of implementing this. Here is a pseudocode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.