http://www.perlmonks.org?node_id=223591


in reply to Re: packing/unpacking/split/join confusion
in thread packing/unpacking/split/join confusion

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
  • Comment on Re: Re: packing/unpacking/split/join confusion

Replies are listed 'Best First'.
Re: Re: Re: packing/unpacking/split/join confusion
by pfaut (Priest) on Jan 01, 2003 at 17:13 UTC

    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 $_.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Re: Re: packing/unpacking/split/join confusion
by poj (Abbot) on Jan 01, 2003 at 17:39 UTC
    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