Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

packing/unpacking/split/join confusion

by sulfericacid (Deacon)
on Jan 01, 2003 at 14:24 UTC ( #223573=perlquestion: print w/ replies, xml ) Need Help??
sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

I have a question or two on packing/unpacking/splitting/joining. I read a lot of messages on here already on packing and unpacking on how everyone else is having problems understanding it as well. Does anyone have a good analogy or scenario on why it's used and how it works?

I just wrote a script which encodes/decodes messages. It encoded perfectly while unpacking("C*".. Using the same method I tried to pack("C".. to decode it but it ran into a problem. It would only decode the first character, the others were deleted somehow. A perl monk told me I had to use split on packing and join on unpacking. This solved the problem, now the entire message can be encoded.

The user tried to explain why this had to be done but I didn't understand it in the least, can someone else try to help explain it? The only piece I got was the reason it didn't work was because my messages had spaces.

Thanks in advance for any and all help!

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Comment on packing/unpacking/split/join confusion
Re: packing/unpacking/split/join confusion
by pfaut (Priest) on Jan 01, 2003 at 14:34 UTC

    Why did you change your template? You specified "C*" with the unpack example and "C" with the pack example. "C*" matches as many characters as are in the string. "C" only matches one character.

    $str = pack('C*',48,49,50,51,52); print "$str\n"; @chars = unpack('C*',$str); print join(', ',@chars),"\n";

    ... results in:

    012345 48, 49, 50, 51, 52
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Sorry, that was a typo. I am using C* in both cases.

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: packing/unpacking/split/join confusion
by poj (Curate) on Jan 01, 2003 at 15:25 UTC
    Ok, here's my try at an explanation
    # Assume the message is this my $message = "me ss age"; # Use unpack to endcode the message one character at a time my @character_code_send = unpack("C*", $message); # You now have an array of ASCII values for each # character of the message. # If you want to send the encoded message you # must join all the element of the array # together first # No need to use join because " " does it for you my $secret = "@character_code_send"; # So what gets sent is this print "Message encoded = $secret\n"; # To decode the receiver has to split back out to an array my @character_code_received = split ' ',$secret; # And then use pack to decode each element back to string my $characters_received = pack("C*", @character_code_received); # To get the answer !! print "Message decoded = $characters_received";

    I assume this is just a programming exercise and not a real attempt at encryption
    HTH poj
      Thanks, I pretty much understand all of that now :) You made everything seem a lot easier, thanks! I have a question though. Are you saying split ' ',$secret; somehow breaks a scalar into an array of different values? If that's what it is, I understand that part of it, but what does ' ' have anything to do with it?

      Thanks!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        The first argument to split is a regex that describes the delimiters in the second argument. By specifying a space, you are telling split to break the string apart wherever it sees a space. So, split ' ',"A B C D" returns ('A','B','C','D'). If instead of space there were commas in your string, you could split ',',"A,B,C,D" instead. This would produce the same result.

        --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
        I think what is confusing you is "Where are the spaces coming from ?"
        # If this is an array @a = ("A","B","C"); # then print "@a"; # result =A B C ie. all spaced out # # because as the Camel book says # Array variables are interpolated into double # quoted strings by joining all the elements # of the array with the delimiter specified # in the $" variable (which is space by default) # #This however print join ('',@a); # result =ABC

        poj
Re: packing/unpacking/split/join confusion
by bart (Canon) on Jan 01, 2003 at 20:44 UTC
    I read a lot of messages on here already on packing and unpacking on how everyone else is having problems understanding it as well. Does anyone have a good analogy or scenario on why it's used and how it works?
    There's a tutorial doc on pack/unpack that may even come with newer versions of Perl: perlpacktut

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-07-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (169 votes), past polls