Re: HoA building



On Wed, Dec 27, 2006 at 10:58:40PM -0700, Chad Perrin wrote:
On Wed, Dec 27, 2006 at 07:42:59PM -0500, M. Lewis wrote:
John W. Krahn wrote:
M. Lewis wrote:

while (my $ln = <DATA>){
chomp $ln;
my ($prod, $flavor) = split /\s/, $ln, 2;

You probably should use ' ' instead of /\s/ as the first argument to split:

my ($prod, $flavor) = split ' ', $ln, 2;

Ok, but why? Are they not the same?

No, they're not. ' ' is a literal space. /\s/ matches any whitespace.

Actually, on second thought, I seem to recall that ' ' has a meaning in
split() beyond a literal space. Unfortunately, I don't recall exactly
what that is. I've tried looking it up using perldoc -f split and in
several books I have here, and I've tried testing it in simple Perl
scripts. The end result is that I've got nothin' -- except that it so
far seems to be acting just like /\s/.

I'd appreciate it if someone on this list would remind me about the
differences. Both ' ' and /\s/ seem to be matching multiple whitespace
characters when used with split() with no differences, including
matching spaces, tabs, and newline characters.

Example:

$ perl -le "@foo = split ' ', qq( one two three\t\n four ); print
@foo;"
onetwothreefour

$ perl -le "@foo = split /\s/, qq( one two three\t\n four ); print
@foo;"
onetwothreefour

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The measure on a man's real character is what he would do
if he knew he would never be found out." - Thomas McCauley
.



Relevant Pages

  • Re: Parenthesis brackets in a regular expression
    ... mean "literal space and tab characters between square ... brackets". ... The meaning of a literal "\t" in a sed expression is ... space, backslash and 't' characters. ...
    (comp.unix.programmer)
  • Re: HoA building
    ... Chad Perrin wrote: ... splitbeyond a literal space. ... matching spaces, tabs, and newline characters. ...
    (perl.beginners)