Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: ${#${(A)=name=word}} behavior depends on number of elements
- X-seq: zsh-workers 39035
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: ${#${(A)=name=word}} behavior depends on number of elements
- Date: Fri, 12 Aug 2016 16:04:07 -0700
- Cc: Lawrence Velazquez <vq@xxxxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject:cc :mime-version; bh=LPXo6SeI70AD9eev7NyHuOkvpJE5LCmtPAIpqY959Mk=; b=Qk6Omb2x+F5U9TmdINmCFCrFel1aTz6hRa9JI+5O2FtByg4XrE00GVtpzg6Q4gyxD4 f1o6JnVQKTj6dsp8uIKnj1aA55k5QDInJJfCB0EykvalFxrgqqis5cWaYZ9xTSU6V/hu 2qAlXnYa1jKvqTxPnzqXHm7PW+7vxLf0YScA+Pl16fNb6qhkX+5LT4fDq9XquL4OzxEN aupqLMPNb1gbE7aYw2OE+cEynjZSCwHgRynuNkrP2Whs6qOQVZLkd+RUgLdmB7FVUluF pjLZXerFnVt698Up39bfD/KOz0uC+X4cFGd5Fvm6hgEtu0ooGElXvX/KTjXonk+V5zBO sBIA==
- In-reply-to: <DC080C9F-3A3D-4B68-BE29-765F7898647F@larryv.me>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <DC080C9F-3A3D-4B68-BE29-765F7898647F@larryv.me>
On Aug 12, 6:11pm, Lawrence Velazquez wrote:
} Subject: ${#${(A)=name=word}} behavior depends on number of elements
}
} As I would expect, ${#${(A)=name=word}} expands to the number of
} elements in array "foo" after assignment.
}
} However, if "foo" ends up with just one element, the expression expands
} to the number of characters in that element.
The value of ${foo=right-hand-side} is the value of right-hand-side,
after applying all the expansion flags, not the value of $foo. Thus
${#...} counts right-hand-side, and the shwordsplit of a word with no
spaces is a scalar, not an array, so the number of characters results.
However, this turns out to be ridiculously easy to change so that
the code matches the implication of the doc for ${NAME=WORD}.
diff --git a/Src/subst.c b/Src/subst.c
index 99e1650..c61551b 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2897,6 +2897,7 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
aval = paramvalarr(pm->gsu.h->getfn(pm), hkeys|hvals);
} else
setaparam(idbeg, a);
+ isarr = 1;
} else {
untokenize(val);
setsparam(idbeg, ztrdup(val));
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 460a841..37166fa 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -82,6 +82,11 @@
>wasnull2d
>wasnull2d
+ unset array
+ print ${#${(A)=array=word}}
+0:${#${(A)=array=word}} counts array elements
+>1
+
(print ${set1:?okhere}; print ${unset1:?exiting1}; print not reached;)
(print ${null1?okhere}; print ${null1:?exiting2}; print not reached;)
1:${...:?...}, ${...?...}
Messages sorted by:
Reverse Date,
Date,
Thread,
Author