unpack - strange behaviour

Discussion in 'Perl Misc' started by ifiaz, Oct 13, 2003.

  1. ifiaz

    ifiaz Guest

    The following program run:

    ($field1, $field2, $field3, $field4, $field5) = unpack("A4 A1 A3 A1
    A*", "this is v strange");

    print "|$field1|\n";
    print "|$field2|\n";
    print "|$field3|\n";
    print "|$field4|\n";
    print "|$field5|\n";

    and the result is this:

    |this|
    ||
    |is|
    |v|
    | strange|

    But, I expect a space for $field2 i.e. | |

    Why $field4 can print with a single character and
    $field2 can't. Am I missing something?
     
    ifiaz, Oct 13, 2003
    #1
    1. Advertising

  2. ifiaz

    Anno Siegel Guest

    ifiaz <> wrote in comp.lang.perl.misc:
    > The following program run:
    >
    > ($field1, $field2, $field3, $field4, $field5) = unpack("A4 A1 A3 A1
    > A*", "this is v strange");
    >
    > print "|$field1|\n";
    > print "|$field2|\n";
    > print "|$field3|\n";
    > print "|$field4|\n";
    > print "|$field5|\n";
    >
    > and the result is this:
    >
    > |this|
    > ||
    > |is|
    > |v|
    > | strange|
    >
    > But, I expect a space for $field2 i.e. | |
    >
    > Why $field4 can print with a single character and
    > $field2 can't. Am I missing something?


    Yes. Perldoc -f pack:

    as necessary. When unpacking, "A" strips
    trailing spaces and nulls, "Z" strips

    Use "a" instead.

    Anno
     
    Anno Siegel, Oct 13, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    433
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,456
    Mike Treseler
    Dec 16, 2004
  3. David Cantin

    Strange behaviour with perl and apache

    David Cantin, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    461
    Jim Gibson
    Nov 3, 2003
  4. Dennis Johansson
    Replies:
    1
    Views:
    503
    Dennis Johansson
    Aug 21, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    396
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page