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

Re: array expansion (Was: [bug-report] brace_ccl and $'\0' in ranges)



Stephane Chazelas wrote:
> I've tried subscribing my other address at yahoo.fr, let's see.

Didn't seem to work.  Here's what SpamAssassin says about your mail; it
ends up with a sizeable negative score, so I doubt if this helps.

X-Spam-Status: No, hits=-8.6 required=5.0
         tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT,
         RCVD_IN_OSIRUSOFT_COM,REFERENCES,REPLY_WITH_QUOTES,
         USER_AGENT_MUTT
         autolearn=ham version=2.55
X-Spam-Level: 
X-Spam-Checker-Version: SpamAssassin 2.55 (1.174.2.19-2003-05-19-exp)

> What is the historical reason why
> 
> array=(1 "" "3 4")
> cmd $array
> # is cmd 1 "3 4"
> 
> the empty arguments are stripped.

I suppose it's because the same thing happens with individual scalars.
An array element basically behaves as a scalar.  (This is similar to other
shells.)

> I understand that zsh arrays are not hashes as in ksh or bash,
> and always start at index 1, even if you only define array[12],
> but wasn't it simpler to have:
> 
> array=(1 "" "3 4")
> cmd $array
> # is cmd 1 "" "3 4"
> # and
> cmd ${array:#}
> # is cmd 1 "3 4"
> 
> I more often need to use "${array[@]}" expansion, than the
> $array one.
> Note that the user guide is clear about this behavior, but not
> the manual. It says:
> 
> | A subscript of the form `[*]' or `[@]' evaluates to all elements
> | of an array; [...] When an array parameter is referenced as
> | `$NAME' (with no subscript) it evaluates to `$NAME[*]'
> 
> no reference about the empty elements implicit filtering.
> 
> Same thing for
> var=
> cmd $var
> # turned into cmd
> # instead of  cmd ""
> cmd ${var+$var}
> # would have made it.
> 
> I end up having to use "$var" and "${array[@]}" everywhere as in
> other shells.

Yes, that's the long and short of it, if you need to preserve empty
array elements.

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>
Work: pws@xxxxxxx
Web: http://www.pwstephenson.fsnet.co.uk



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