in reply to A good lesson on Tie (through a negative example)

Good post++

One words of caution though. If you take another look at my post at Re: sorting a vec, you'll find that I have replaced the code

print unpack '(B4)*', $vec;

With

print map{ vec( $vec, $_, 4) } 0 .. 99;

The reason is that the first example doesn't do what I originally thought, and not what I still think would be the most intuative reading of the code.

Using the unpack format '(B4)*', doesn't return every set of 4 bits from the string. It returns every other 4-bits from the string! That is, the output consists of a list of numbers, the value of each being determined from most significant 4-bits of each byte of the input string, with the other 4-bits being summarily discarded. (*)

Only noticed this a while after posting (I noticed that the string I had carefully packed 100 4-bit fields into was only outputting 50 values when printed with the original code).

This was changed within about 1/2 an hour of the original post, at a time when then CB was quiet, and there were only 8 people listed in others users. You must have grabbed the code pretty quickly after I posted the original.

I apologise for my error. I don't believe that this affects the information in your post, but if it does, feel free to transfer all the blame to me.

*It also worth noting that '(b4)*', also grabs the same, most significant, 4-bits of each byte, with the others being discarded -- but it starts processing the bytes in the string from the other end. Very confusing and non-intuative.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!

Replies are listed 'Best First'.
Re: Re: A good lesson on Tie ( My error!)
by pg (Canon) on Nov 02, 2003 at 06:03 UTC

    Thanks!

    So I stole again ;-) Okay I replaced that syntax everywhere in my post.

    But this time I modified your code a little bit, and used this:

    print join(" ", map{ vec( $vec, $_, 4) } 0 .. 99);

    Without those spaces in between those numbers, I was quite confused (visually), as now the numbers no longer have uniformed length, some are 1 digit, when others are two-digits. If I saw 15 on screen, I really don't know whether it is 1 and 5, or 15.

    As you guessed, that error does not affect the content of my meditation, but it is always a good practice to make things better and more precise.