Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This is that author speaking. Thanks for the e-mail! I'd be delighted if you used my code. It would be a wonderful way to commemorate the end of an error. :)

What you found on the web is probably hideously out of date, however. Here is the latest, self-testing version.

#!/usr/bin/perl -w # Pure-perl LZW implementation. The implementation is Copyright 2003 # by Rocco Caputo. Where applicable, all rights are reserved. The # author makes no claim of rights to the LZW algorithm, which is # patented by UniSys, or something. # # This implementation is being released under the same terms as Perl # itself. use warnings; use strict; my $original_char_stream = <<'EOS'; Intra-universe security is easy enough. Inter-universe security will be hard. I have half an idea for something involving audit trails that may work. It may be necessary to backtrack and validate an object recorded path before executing the object. For example, imagine USENET if each host in the Path header was asked to vouch for the object having been there. The ratio of positive answers over total hosts in the trail would determine a "percent of certainty". Combined with an average "percent of trustability" for each host, the current universe would come up with a "total trustability" percent to test against a threshhold. EOS ### Shared ########################################################### +######### my @code_stream; ### Encoding. ######################################################## +######### { my %dict; my $next_code = 256; $dict{chr $_} = $_ for 0..255; my $code = ""; my $c_index = 0; while ($c_index < length($original_char_stream)) { my $next_char = substr($original_char_stream, $c_index++, 1); my $possible_code = $code . $next_char; if (exists $dict{$possible_code}) { $code = $possible_code; } else { push @code_stream, $dict{$code}; $dict{$possible_code} = $next_code++; $code = $next_char; } } push @code_stream, $dict{$code}; } ### Decoding. ######################################################## +######### my $decoded_char_stream = ""; { my %dict; my $next_code = 256; $dict{$_} = chr($_) for 0..255; my $code_word = shift @code_stream; $decoded_char_stream .= $dict{$code_word}; while (@code_stream) { my $previous_word = $dict{$code_word}; $code_word = shift @code_stream; if (exists $dict{$code_word}) { my $decoded_word = $dict{$code_word}; $decoded_char_stream .= $decoded_word; $dict{$next_code++} = $previous_word . substr($decoded_word, 0, 1); } else { my $decoded_word = $previous_word . substr($previous_word, 0, 1) +; $decoded_char_stream .= $decoded_word; $dict{$next_code++} = $decoded_word; } } } ### Final test. ###################################################### +######### if ($original_char_stream eq $decoded_char_stream) { print "Compressed and decompressed strings match! :)\n"; } else { print "Compressed and decompressed strings don't match! :(\n"; }

-- Rocco Caputo - troc@pobox.com - poe.perl.org


In reply to Re: Re: Any news on Compress::LZW? by rcaputo
in thread Any news on Compress::LZW? by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found