When writing the script posted in this reply I originally tried to use unpack to extract each line after sorting but it mashed the output into a single line with no line feeds. (I went with substr instead). The documentation states that unpack does the reverse of pack but using the 'A' template seems to lose trailing newline characters when unpacking. Embedded newlines are preserved. Using the 'a' template instead works as expected.

use strict; use warnings; use feature qw{ say }; use List::Util qw{ max }; my $string = qq{abc\n}; my $packed = pack q{A*}, $string; my $unpacked = unpack q{A*}, $packed; say $string eq $packed ? q{OK - original and packed are the same} : q{Not OK - original and packed differ}; sideBySide( $string, $packed ); say $string eq $unpacked ? q{OK - original and unpacked are the same} : q{Not OK - original and unpacked differ}; sideBySide( $string, $unpacked ); say q{=} x 50; $string .= qq{def\n}; $packed = pack q{A*}, $string; $unpacked = unpack q{A*}, $packed; say $string eq $packed ? q{OK - original and packed are the same} : q{Not OK - original and packed differ}; sideBySide( $string, $packed ); say $string eq $unpacked ? q{OK - original and unpacked are the same} : q{Not OK - original and unpacked differ}; sideBySide( $string, $unpacked ); sub sideBySide { my( $original, $modified ) = @_; my @origChars = map { sprintf q{%#02x}, ord } split m{}, $original +; my @modChars = map { sprintf q{%#02x}, ord } split m{}, $modified +; my $nRows = max scalar( @origChars ), scalar( @modChars ); for ( 1 .. $nRows ) { printf qq{%8s%8s\n}, scalar @origChars ? shift @origChars : q{}, scalar @modChars ? shift @modChars : q{}; } }

The output.

OK - original and packed are the same 0x61 0x61 0x62 0x62 0x63 0x63 0xa 0xa Not OK - original and unpacked differ 0x61 0x61 0x62 0x62 0x63 0x63 0xa ================================================== OK - original and packed are the same 0x61 0x61 0x62 0x62 0x63 0x63 0xa 0xa 0x64 0x64 0x65 0x65 0x66 0x66 0xa 0xa Not OK - original and unpacked differ 0x61 0x61 0x62 0x62 0x63 0x63 0xa 0xa 0x64 0x64 0x65 0x65 0x66 0x66 0xa

My questions: is this a bugette, a feature, or am I having a senior moment?

Cheers,

JohnGG


In reply to Problem with pack/unpack asymmetry by johngg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.