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

Re: 3.1.9-dev-6: bug or confusion?



On Sep 13,  2:44pm, E. Jay Berkenbilt wrote:
} Subject: 3.1.9-dev-6: bug or confusion?
}
} Define these two functions:
} 
} function okay
} {
}   emulate -L zsh
}   echo ${#${@:#-}}
}   setopt localoptions xtrace
}   (( ${#${@:#-}} ))
} }
} 
} function broken
} {
}   emulate -L zsh
}   echo ${#${@#-}}
}   setopt localoptions xtrace
}   (( ${#${@#-}} ))
} }
} 
} zsh% broken -
} 0
} +broken:5> ((  1  ))
} 
} Why would the value be 1 inside (( ... )) and 0 outside in the second
} case but not in the first?  Does it have something to do with an empty
} string being counted as a word in one case but not the other?

Yes, it has something to do with that.

    (( stuff ))
    
is equivalent to

    let "stuff"

(note the double quotes).  So the two expansions in `broken' should be
akin to the difference between $@ (without quotes) and "$@".

Where it gets confusing is that, in the nested expansion ${#${@}}, you
first have the expansion of the inner $@, which (when not in quotes)
implies that the empty elements disappear, and then the count is done
on the remaining array.  This is not the same as ${#@}, which counts
the array *before* expanding it (see below).

Aside to EJR:  I should have remembered this.

Finally, in ${@#-}, the pattern match/delete on each word is performed
*before* the expansion, so again the resulting empty elements (if any)
disappear only when the whole expression is not in quotes.

Aside:  This particular item is missing from the 11 "Rules" of parameter
expansion in the Expansion chapter of the zsh manual.  It happens after
step 7, at approximately the same time as step 8 (because empty elements
resulting from an (s) modifier are completely removed, even in quotes,
but separators inserted by (j) are always added between the empties).
Also missing is an exact indication of when ${^...} ${#...} and ${~...}
are applied; ${#...} actually happens between steps 6 and 7, whereas the
other two don't happen until after step 8.  (Care to fix this up in a
more accurate way in the docs, Peter?)

The definition of ${...:#...} says explicitly that matched elements are
completely removed from the array, so that's why there's no difference
in the `okay' case.  (And that happens at step 6, before (j).)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



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