* Re: array expansion (Was: [bug-report] brace_ccl and $'\0' in ranges)
[not found] <20031007081417.GA418@pcchazelas.free.fr>
@ 2003-10-10 20:16 ` Peter Stephenson
0 siblings, 0 replies; only message in thread
From: Peter Stephenson @ 2003-10-10 20:16 UTC (permalink / raw)
To: Stephane Chazelas; +Cc: Zsh hackers list
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@pwstephenson.fsnet.co.uk>
Work: pws@csr.com
Web: http://www.pwstephenson.fsnet.co.uk
^ permalink raw reply [flat|nested] only message in thread