in reply to Convert ASCII string to 7-bit binary string

Define better. More readable/maintainable? Faster? Algorithmically more elegant? I'm always a huge fan of "works," which your code does.
print my $out = join '', map {substr unpack('B8'), 1} split //, '4B';
substr, unpack
print my $out = join '', map {sprintf '%07b', ord} split //, '4B';
sprintf, ord

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Convert ASCII string to 7-bit binary string
by Anonymous Monk on Oct 27, 2015 at 20:03 UTC
    why is substr needed there?
      substr $var, 1 returns the string in $var minus the first character. That's what converts the 8 character binary output from unpack into the desired 7 character one. You could do the same thing with a substitution with the /r modifier, and I'm sure a number of other methods. In the OP, it's handled with s/^.//;, but then you can't chain.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.