Re: HoA building
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Thu, 28 Dec 2006 01:48:59 -0500
"CP" == Chad Perrin <perrin@xxxxxxxxxxxx> writes:
CP> 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?
CP> No, they're not. ' ' is a literal space. /\s/ matches any whitespace.
' ' is not a literal space when used with split. first off, the first
argument to split is always a regex so use // instead of ''. but ' ' is
special (and only when in quotes) and it has several benefits:
from perldoc -f split:
As a special case, specifying a PATTERN of space (' ') will
split on white space just as "split" with no arguments does.
Thus, "split(' ')" can be used to emulate awk's default
behavior, whereas "split(/ /)" will give you as many null
initial fields as there are leading spaces. A "split" on
"/\s+/" is like a "split(' ')" except that any leading
whitespace produces a null first field. A "split" with no
arguments really does a "split(' ', $_)" internally.
uri
--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.
- References:
- HoA building
- From: "M. Lewis"
- Re: HoA building
- From: John W. Krahn
- Re: HoA building
- From: "M. Lewis"
- Re: HoA building
- From: Chad Perrin
- HoA building
- Prev by Date: Re: HoA building
- Next by Date: Re: HoA building
- Previous by thread: Re: HoA building
- Next by thread: Re: HoA building
- Index(es):