sfpug_perl_split_regexp_minutia

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: sfpug@sf.pm.org
From: Mark Kvale <kvale@phy.ucsf.EDU>
Subject: Re: [sf-perl] stuck in a rut with split
Date: Sun, 3 Jun 2001 12:11:50 -0700

On Sunday 03 June 2001 00:59, you wrote:
> its late and I cant figure this one out,
>
> what does
>
> $n = split /./, 'foobar';
>
> produce and why?
>
> without trying it out do you know what the answer is?
>
> [A] 13
> [B] 12
> [C] 2
> [D] 1
> [E] 0
>

The answer is 0. split does a global match of the regexp against the string 
and returns an array of all the bits of string _between_ the matches. Since 
/./ matches every character in the string, there is nothing in between, empty 
trailing array elements are deleted, and an array with no elements is thus 
produced. Scalar context turns this into $n = 0.

If you are trying to split a string into individual chars, try 

   $n = split //, 'foobar';

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu