Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: field splitting with empty fields



On Tue, 30 Oct 2007 00:20:48 -0400
Clint Adams <clint@xxxxxxx> wrote:
> On Mon, Oct 29, 2007 at 05:13:55PM -0700, Bart Schaefer wrote:
> > print ${list//:/$'\n'}
> > 
> > ??
> 
> Okay, I asked a stupid question.
> 
> I want the line split into words according to fields which may be
> blank.  Right now I'm substituting :x: for :: and then removing 
> the x's to ensure that the array value match up to the proper
> indices.  This seems suboptimal.

It's odd it removes the space even if quoted:

% foo="one:two::four:five"
% print -l "${(@s.:.)foo}"
one
two
four
five

(the "@" doesn't make a difference, it's just to make the point.)  This
seems to be deliberate in paramsubst() in subst.c: isarr is set to 2 in
this case, and there's a special test:

		if (qt && !*y && isarr != 2)
		    y  = dupstring(nulstring);
		else {
		    y = dupstring(x);
		    if (globsubst)
			shtokenize(y);
		}

i.e. add a special empty string if it's quoted (which it is) and the string
is empty (which it is) and isarr is not 2 (but it is).  That first branch
means the argument won't be removed due to being empty; since we don't
take it, it will be removed.

The only comment on isarr is by me at the top of the function speculating
on what it actually means (unfortunately most of the comments in the
function were added much later by me along the lines of "I don't really
know what this does or why it's so obscure or why the author couldn't be
bothered to explain it") and I think I got it wrong.  I think it's to do
with this case and its relatives, i.e. splitting into an array.  However,
why we want to ignore the effect of the double quotes here is not clear to
me.  I can't see anything in the manual which would explain it, either.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



Messages sorted by: Reverse Date, Date, Thread, Author