in reply to perl bit matrix transposition / weak typing problem

I think that you are going about this in a too C-ish manner. Whenever I encounter a problem where Perl seems to be getting in the way, I tend to find that I'm using the wrong method and by looking around, there is a perlish method or function that addresses the problem.

In this case, you need vec.

#! perl -slw use strict; sub xformBits { my( $in ) = shift; my $out = chr(0) x length( $in ); vec($out,$_,1) = vec($in,7-int($_/8)+8*(7-$_%8),1) for 0..63; return $out; } my $example = 'ABCDEFGH'; print "Input: $example"; print join ' ', split'', unpack 'B8', $_ for split '', $example; my $xformed = xformBits $example; print "\nOutput: $xformed"; print join ' ', split'', unpack 'B8', $_ for split '', $xformed; __END__ [12:12:24.25] P:\test>414344 Input: ABCDEFGH 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 1 0 0 1 0 0 0 Output:   ??f¬ 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 [12:12:49.51] P:\test>

Examine what is said, not who speaks.        The end of an era!
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: perl bit matrix transposition / weak typing problem
by infi (Acolyte) on Dec 13, 2004 at 12:51 UTC

    Hooray! Yes, vec() was the function I was looking for, indeed. This will avoid the bitstring solution altogether, something which nearly everyone has recommended to me up to this point.

    Thank you, BrowserUk, for restoring my faith and showing me the light. I was beginning to wonder if I would have to wait until perl6 (and hope that it included stronger typing in this fashion..) I'll work on the C-ish thinking, as well.

    Off to check perldelta to see when vec() arrived...

    Cheers,
    -Jason