[kw-pm] huffman

fishbot eric at uc.org
Sun Jun 18 16:29:39 PDT 2006


Without anything obscure or low level?  Reading a bit at a time
is pretty obscure, at least in Perl-land.

I didn't read the file a bit at a time, I actually pulled it in
several kbytes at a time, then iterated over the bits with vec(),
like so:

      for ( 0 .. $bits )
      {
         $treeloc = vec( $buff, $_, 1 ) ?
                     $treeloc->{Left} :
                     $treeloc->{Right};

         if ( $treeloc->{Term} )
         {
            print $fhout $treeloc->{Char};
            $treeloc = $htree;
         }
      }

Where $buff was my filebuffer, and the rest is fairly
self-documenting-ish.

vec() is quite efficient, but looping over the bits in a
multi-mbyte file is pushing perl's limits.

If you are looking to handle something other than text IO in
Perl, you likely need pack(), unpack() or vec(), or some
combination thereof.  Though ultra-powerful, they are also the
posterchildren for obscure low level.

fishbot


---- original message : 2006-06-17 2:18pm : lloyd carr ----

As usual I am happily distracted from doing anything useful by one of
fishbot's talks :)

How do you read a file one bit at a time? Can you without going to some
obscure low level stuff???

- Lloyd
_______________________________________________
kw-pm mailing list
kw-pm at pm.org
http://mail.pm.org/mailman/listinfo/kw-pm


More information about the kw-pm mailing list