zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Cc: Lawrence Velazquez <vq@larryv.me>
Subject: Re: ${#${(A)=name=word}} behavior depends on number of elements
Date: Fri, 12 Aug 2016 16:04:07 -0700	[thread overview]
Message-ID: <160812160407.ZM30052@torch.brasslantern.com> (raw)
In-Reply-To: <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:${...:?...}, ${...?...}


      reply	other threads:[~2016-08-12 23:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-12 22:11 Lawrence Velázquez
2016-08-12 23:04 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160812160407.ZM30052@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=vq@larryv.me \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).