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

Re: ${#${(A)=name=word}} behavior depends on number of elements



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