Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!uunet!mcsun!hp4nl!alchemy!piet From: piet@cs.ruu.nl (Piet van Oostrum) Newsgroups: comp.lang.perl Subject: Re: Split question Message-ID: <1991Mar01.090744.6395@cs.ruu.nl> Date: 1 Mar 91 09:07:44 GMT References: <1991Feb27.185226.7279@uvaarpa.Virginia.EDU> Sender: piet@cs.ruu.nl (Piet van Oostrum) Reply-To: piet@cs.ruu.nl (Piet van Oostrum) Organization: Dept of Computer Science, Utrecht University, The Netherlands Lines: 35 In-Reply-To: bjaspan@athena.mit.edu (Barr3y Jaspan) >>>>> bjaspan@athena.mit.edu (Barr3y Jaspan) (BJ) writes: BJ> print "$]\n"; BJ> @_ = split(/ *(0[^0]+0) *| /, '0foo bar0 baz frob 0la la la0 quux'); BJ> print join(', ', @_),"\n"; BJ> produces the following output: BJ> $Header: perly.c,v 3.0.1.9 90/11/10 01:53:26 lwall Locked $ BJ> Patch level: 41 BJ> , 0foo bar0, baz, , frob, 0la la la0, quux BJ> Why is there a null entry between baz and frob in the returned array? Perl apparently always puts a 'separator' entry between the split entities, even if the particular separator didn't match the part with parentheses (this is the same with normal regexp matching). So if the separator is a space the part between parentheses matches the null string. I think it would be nicer if perl didn't include the separator in this case, but that might be too difficult. It also would make it different from $1, $2,... behaviour. The solution is simply to weed out the null entries if there can't be any legal null entries in the data (which is not the case in the above example - there will be a null entry if there are two successive spaces, but that could be a mistake). Anyway, the following might be what you mean: @_ = grep(!/^$/, split(/ *(0[^0]+0) *| +/, '0foo bar0 baz frob 0la la la0 quux')); print join('|', @_),"\n"; -- Piet* van Oostrum, Dept of Computer Science, Utrecht University, Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands. Telephone: +31 30 531806 Uucp: uunet!mcsun!ruuinf!piet Telefax: +31 30 513791 Internet: piet@cs.ruu.nl (*`Pete')