Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Pack documentation

by Anonymous Monk
on Jun 11, 2014 at 02:41 UTC ( [id://1089468]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm reading the documentation of 'pack'. I cannot understand this:
unpack("a3/A A*", "007 Bond J ") gives (" Bond", "J") unpack("a3 x2 /A A*", "007: Bond, J.") gives ("Bond, J", ".")
"You write "/"sequence-item and the repeat count is obtained by popping off the last element from the stack. The sequence-item must not have a repeat count. If sequence-item refers to a string type ("A", "a", or "Z"), the length-item is the string length, not the number of strings."
What does it all mean? I think when the doc talks about popping from the stack, that only applies to the numeric data. Or something. So yeah, how does that work?

Also:

"Pack and unpack can operate in two modes: character mode ("C0" mode) where the packed string is processed per character, and UTF-8 mode ("U0" mode) where the packed string is processed in its UTF-8-encoded Unicode form on a byte-by-byte basis."
What's that all about? It means that with U0 pack treats the string as C-style bytearray? Or what?

Replies are listed 'Best First'.
Re: Pack documentation
by choroba (Cardinal) on Jun 11, 2014 at 09:18 UTC
    a3/A takes the first three bytes and interprets them as a number (7 in this case). It then reads 7 bytes and interprets them as a space padded string (A). So, ' Bond  ' is shortened to ' Bond' only. The following A* just takes the rest as a string, removing trailing spaces. The second example is left as an exercise for the reader.

    I don't understand your third question. Could you rephrase? Have you studied the examples in the paragraph that follows your citation?

    $ perl -CS -E 'say "\x{3B1}\x{3C9}"' | perl -CS -ne 'printf "%v04X\n", $_ for unpack("C0A*", $_)' 03B1.03C9 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' | perl -CS -ne 'printf "%v02X\n", $_ for unpack("U0A*", $_)' CE.B1.CF.89 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' | perl -C0 -ne 'printf "%v02X\n", $_ for unpack("C0A*", $_)' CE.B1.CF.89 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' | perl -C0 -ne 'printf "%v02X\n", $_ for unpack("U0A*", $_)' C3.8E.C2.B1.C3.8F.C2.89
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Pack documentation (unpack U0 C0 / repeat)
by Anonymous Monk on Jun 11, 2014 at 03:32 UTC
Re: Pack documentation
by LanX (Saint) on Jun 11, 2014 at 02:50 UTC
    > I'm reading the documentation of pack .

    Did you already have a look into perlpacktut ?

    Cheers Rolf

    (addicted to the Perl Programming Language)

      Yes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1089468]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found