Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: packing/unpacking/split/join confusion

by poj (Monsignor)
on Jan 01, 2003 at 15:25 UTC ( #223578=note: print w/replies, xml ) Need Help??

in reply to packing/unpacking/split/join confusion

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

Replies are listed 'Best First'.
Re: Re: packing/unpacking/split/join confusion
by sulfericacid (Deacon) on Jan 01, 2003 at 17:04 UTC
    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?


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


      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';
        By specifying a space, you are telling split to break the string apart wherever it sees a space.

        Almost. A literal space is a special case. It splits on whitespace like /\s+/ but it doesn't return a null field when there is leading whitespace. So, it is just like split with no arguments but it allows you to specify the variable instead of using $_.

        "My two cents aren't worth a dime.";
      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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://223578]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-03-23 20:58 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (296 votes). Check out past polls.