Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Converting Stream of bits back into Stream of Characters it Encodes

by AllyClark (Novice)
on Jun 10, 2002 at 18:42 UTC ( [id://173254]=perlquestion: print w/replies, xml ) Need Help??

AllyClark has asked for the wisdom of the Perl Monks concerning the following question:

I have been grappling with this snippet all morning and it is not doing what I think it should do. This is my first post also. Any help would be very much appreciated - thanks!-

sub huff_decode ( my $tree =shift; my $get_bit =shift; my $put_stream =shift; my $cur_node = $tree; my $cur_bit; while( defined(cur_bit = &$get_bit ) ) { $cur_code = $cur_bit, $cur_node->right : $cur_node->left; unless( $cur_node->left ) { &$put_stream( $cur_node->value ); $cur_node = $tree; } } }
  • Comment on Converting Stream of bits back into Stream of Characters it Encodes
  • Download Code

Replies are listed 'Best First'.
Re: Converting Stream of bits back into Stream of Characters it Encodes
by belden (Friar) on Jun 10, 2002 at 18:59 UTC
    It looks like you have a typo in your ternary statement:

    $cur_node = $cur_bit ? $cur_node->right : $cur_node->left;

    You've got a , where it looks like you mean to use a ? instead.

    But I could be way off base on this one: I don't know much about Huffman encoding. If you post more of your code, some other monks might dig in and offer some more meaningful advice.

    hth, and welcome to the monastery!
    blyman
    setenv EXINIT 'set noai ts=2'

(ichi) Re: Converting Stream of bits back into Stream of Characters it Encodes
by ichimunki (Priest) on Jun 10, 2002 at 19:49 UTC
    This code looks suspiciously like the examples for Huffman Encoding in "Mastering Algorithms with Perl". I'm not sure what you think it should do, but the ',' in there is not valid syntax and should be replaced with a '?'. You've also got a $cur_code where you want a $cur_node. For your typo convenience, all of the examples in the book are available in .tar.gz or .zip format from O'Reilly's web site.

    We really need to see what you constructed for the get_bit() and put_stream() routines and how you constructed your hash tree.

    If you still don't understand how this works after you get the typos fixed, I'm sure we can discuss how this walks down a tree looking for nodes, checking whether a node contains a value or a link/reference to another node, and outputting any values found or following the link/reference to the next node depending on what it finds in the current node. :)

Re: Converting Stream of bits back into Stream of Characters it Encodes
by stajich (Chaplain) on Jun 10, 2002 at 19:09 UTC
    I hope this isn't some school assignment question trollling...

    You're leaving a lot of information out of the question, but one can guess that you want to do huffman decoding.

    I assume that your huffman tree has nodes with methods left,right, value. So $node->value() is probably going to return a binary number - I would imagine in fact that your put_stream method is where this should go but basically you just want to do: chr(oct("0b".$cur_node->value)); See 15577 for info on converting to decimal from binary. Some error checking to make sure you aren't producing a number > 255 before calling chr would be in order too.

    Can't do a whole lot else to help without some more info.

    Update: Course as kal says, need to know the endianess of your data otherwise that won't work.

Re: Converting Stream of bits back into Stream of Characters it Encodes
by kal (Hermit) on Jun 10, 2002 at 18:57 UTC

    All code correctly solves some problem. It all does something. But, without telling us what problem you're trying to solve, we're unlikely to be able to tell you what is wrong with the code :)

    Exactly what is the above supposed to do?

      Sorry - in case the above sounded a bit sniffy, you've not told us:

      • Whether you are converting bits to bytes, or de-huffman coding,
      • What endianness the data is,

      ...etc... - there could be many, non-obvious, reasons that code isn't working.

Re: Converting Stream of bits back into Stream of Characters it Encodes
by Fastolfe (Vicar) on Jun 10, 2002 at 22:38 UTC
    This code does not even compile. A lot of the advice you've been given in this thread kind of depends on the fact that what you have should do something as it's written. With use strict and use warnings in effect, I get lots of errors. Let's be sure that these errors aren't the cause of your problem first.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173254]
Front-paged by JSchmitz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (None)
    As of 2024-04-25 04:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found