in reply to unpack in incrementing variables

As you said, use (s)printf. (printf doesnt respect -l so this looks a touch more clumsy than it needs to).

D:\dev>perl -e "printf '%08b%s',$_,$/ for 1..10" 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 00001010

Unpack operates on strings not on integers so you need to ensure that chr(10) is passed in and not '10'.

D:\dev>perl -le "print unpack 'B*',chr($_) for 1..10" 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 00001010
---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^2: unpack in incrementing variables
by GrandFather (Saint) on Oct 14, 2005 at 09:38 UTC

    Thank you. At last the answer to the question I actually asked. The question was why, not how. The answer is "Unpack operates on strings not on integers".


    Perl is Huffman encoded by design.

      I think people were inclined to show the how because the why is in the docs:

      unpack TEMPLATE,EXPR
      "unpack" does the reverse of "pack": it takes a string and expands it out into a list of values. (In scalar context, it returns merely the first value produced.)

      But I did it because i hadnt had my coffee before I posted (was waiting for it finish being made) and didnt read the question properly. Luckily the coffee was finished just after I hit submit so I realized the error and fixed it right after. :-)

      ---
      $world=~s/war/peace/g

        Funny how one can read the docs and see what one wants to see. I read that a day ago when I was trying to figure the problem out and thought "I yes, a number is stored as a string of bytes so that should be fine". Argh.


        Perl is Huffman encoded by design.
      The question was why, not how.
      The question "Is it possible to...?" includes an implicit "How?" Answering such question with a simple "yes" is generally seen as teasing.

      Caution: Contents may have been coded under pressure.