Chad Perrin 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


Chad, I've been experimenting with this a bit since your posting. Maybe this will help: (I'm trying to understand the diff too)

perl -le'
my $ln = "one  two  three  four  ";
print map " |$_| ", split /\s/, $ln;
print map " |$_| ", split " ", $ln;
'
 |one|  ||  |two|  ||  |three|  ||  |four|
 |one|  |two|  |three|  |four|


perl -le'
my $ln = "one  two  three  four  ";
print map " |$_| ", split /\s+/, $ln;
print map " |$_| ", split " ", $ln;
'
 |one|  |two|  |three|  |four|
 |one|  |two|  |three|  |four|


--

 Swap read error.  You lose your mind.
  03:25:01 up 14 days, 16 min,  0 users,  load average: 0.36, 0.33, 0.38

 Linux Registered User #241685  http://counter.li.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to