zsh-workers
 help / color / mirror / code / Atom feed
* ${#${(A)=name=word}} behavior depends on number of elements
@ 2016-08-12 22:11 Lawrence Velázquez
  2016-08-12 23:04 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Lawrence Velázquez @ 2016-08-12 22:11 UTC (permalink / raw)
  To: zsh-workers

As I would expect, ${#${(A)=name=word}} expands to the number of
elements in array "foo" after assignment.

	% unset foo; echo ${#${(A)=foo=a 'b c' d}}; typeset foo
	3
	foo=( a 'b c' d )

However, if "foo" ends up with just one element, the expression expands
to the number of characters in that element.

	% unset foo; echo ${#${(A)foo=abc}}; typeset foo    
	3
	foo=( abc )
	% unset foo; echo ${#${(A)=foo=abc}}; typeset foo 
	3
	foo=( abc )
	% unset foo; echo ${#${(A)foo=a b c}}; typeset foo  
	5
	foo=( 'a b c' )
	% unset foo; echo ${#${(A)=foo='a b c'}}; typeset foo
	5
	foo=( 'a b c' )

The behavior is the same for ":=" and "::=".

	% echo $ZSH_PATCHLEVEL
	zsh-5.2-0-gc86c20a
	% setopt
	combiningchars
	extendedhistory
	incappendhistorytime
	interactive
	login
	monitor
	promptsubst
	shinstdin
	zle

vq


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: ${#${(A)=name=word}} behavior depends on number of elements
  2016-08-12 22:11 ${#${(A)=name=word}} behavior depends on number of elements Lawrence Velázquez
@ 2016-08-12 23:04 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2016-08-12 23:04 UTC (permalink / raw)
  To: zsh-workers; +Cc: Lawrence Velazquez

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:${...:?...}, ${...?...}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-12 23:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-12 22:11 ${#${(A)=name=word}} behavior depends on number of elements Lawrence Velázquez
2016-08-12 23:04 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).