Falkkin is working on a Java Huffman encoder...
Moved only by the will to serve, I propose him an
example of what he could do if he did that in Perl... :-)
$"=$/=$_;map{$0{$_}++}split//,$_=<>;@0=map{[$_,$0{$_},$_]}keys%0;
$/=1+1;until(@0==1){@0=sort{$$b[1]<=>$$a[1]}@0;$t=pop@0;$p=pop@0;
push@0,[[$$t[0],$$p[0]],$$t[1]+$$p[1],$$t[$/].$$p[$/]];map{map{$t
{$_}=$;%$/.$t{$_}}split//;$;++}$$t[$/],$$p[$/]}s/(.)/$t{$1}/gs;sub
_{my$t=pop;if(ref$t){my@t=map{if(!ref$$t[$_]){1,unpack"b8",$$t[$_]}
else{0,""}}0,1;"@t[0,$/,1]"._($$t[0])._($$t[1])."$t[-1]"}}print
pack"sB*",length,_($0[0][0]).$_
(This was one of my "helpers" for OPC5)
Corresponding Huffman decoder to be posted soon.
-
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.