zsh-workers
 help / color / mirror / code / Atom feed
* get keys of an associative array?
@ 2017-03-26 19:18 Jens Elkner
  2017-03-26 19:34 ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Jens Elkner @ 2017-03-26 19:18 UTC (permalink / raw)
  To: zsh-workers

Hi,

I've KSH_ARRAYS (beside KSH_GLOB KSH_TYPESET KSH_OPTION_PRINT) set,
however, getting the key set of an array via ${!vname[@]} (as usually
done within ksh or bash) does not work (bad substitution). Couldn't find
anything else, except the @k flag, but this doesn't work as well, e.g.

unset A; typeset -A A; A['a']=1; A['b']=2; A['c']=3 ; print "${(@k)A}"

should print 'a b c' but prints '3'. Bug, feature? Hmmm, any hints?

Thanx,
jel.
-- 
Otto-von-Guericke University     http://www.cs.uni-magdeburg.de/
Department of Computer Science   Geb. 29 R 027, Universitaetsplatz 2
39106 Magdeburg, Germany         Tel: +49 391 67 52768


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

* Re: get keys of an associative array?
  2017-03-26 19:18 get keys of an associative array? Jens Elkner
@ 2017-03-26 19:34 ` Peter Stephenson
  2017-03-26 20:03   ` Bart Schaefer
  2017-03-26 21:55   ` Jens Elkner
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Stephenson @ 2017-03-26 19:34 UTC (permalink / raw)
  To: zsh-workers

On Sun, 26 Mar 2017 21:18:28 +0200
Jens Elkner <jel+zsh@cs.uni-magdeburg.de> wrote:
> I've KSH_ARRAYS (beside KSH_GLOB KSH_TYPESET KSH_OPTION_PRINT) set,
> however, getting the key set of an array via ${!vname[@]} (as usually
> done within ksh or bash) does not work (bad substitution).

That's never been implemented; it may be too far away from how zsh works
to do usefullly.

> Couldn't find anything else, except the @k flag, but this doesn't work
> as well, e.g.
> 
> unset A; typeset -A A; A['a']=1; A['b']=2; A['c']=3 ; print "${(@k)A}"
> 
> should print 'a b c' but prints '3'. Bug, feature? Hmmm, any hints?

You need "${(k)A[@]}" in ksh mode, but I'm not sure why the (@) flag
doesn't do the trick.  As it's an associative array you get the keys
back unordered.

pws


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

* Re: get keys of an associative array?
  2017-03-26 19:34 ` Peter Stephenson
@ 2017-03-26 20:03   ` Bart Schaefer
  2017-03-26 22:06     ` Jens Elkner
  2017-03-26 21:55   ` Jens Elkner
  1 sibling, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2017-03-26 20:03 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 336 bytes --]

On Mar 26, 2017 12:41 PM, "Peter Stephenson" <p.w.stephenson@ntlworld.com>
wrote:


You need "${(k)A[@]}" in ksh mode, but I'm not sure why the (@) flag
doesn't do the trick.



The @ flag only makes things that are already arrays remain arrays in
contexts where they would otherwise be joined.  In ksharrays context, ${A}
is a scalar.

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

* Re: get keys of an associative array?
  2017-03-26 19:34 ` Peter Stephenson
  2017-03-26 20:03   ` Bart Schaefer
@ 2017-03-26 21:55   ` Jens Elkner
  1 sibling, 0 replies; 6+ messages in thread
From: Jens Elkner @ 2017-03-26 21:55 UTC (permalink / raw)
  To: zsh-workers

On Sun, Mar 26, 2017 at 08:34:46PM +0100, Peter Stephenson wrote:
> On Sun, 26 Mar 2017 21:18:28 +0200
> Jens Elkner <jel+zsh@cs.uni-magdeburg.de> wrote:
> > I've KSH_ARRAYS (beside KSH_GLOB KSH_TYPESET KSH_OPTION_PRINT) set,
> > however, getting the key set of an array via ${!vname[@]} (as usually
> > done within ksh or bash) does not work (bad substitution).
> 
> That's never been implemented; it may be too far away from how zsh works
> to do usefullly.

Ah ok, thought initially, can't be so hard to check, whether the first
char after a '${' is a '#' or '!' or ' ' ...  Life would be much easier ;-)
  
> > Couldn't find anything else, except the @k flag, but this doesn't work
> > as well, e.g.
> > 
> > unset A; typeset -A A; A['a']=1; A['b']=2; A['c']=3 ; print "${(@k)A}"
> > 
> > should print 'a b c' but prints '3'. Bug, feature? Hmmm, any hints?
> 
> You need "${(k)A[@]}" in ksh mode, but I'm not sure why the (@) flag
> doesn't do the trick.

Ahh, yes - this works. Also found out, that if I 'setopt no_KSH_ARRAYS'
"${(@k)A}" works as well (but is inconvinient).

> As it's an associative array you get the keys
> back unordered.

Yepp. That's expected for hash sets.

Thanx a lot,
jel.
-- 
Otto-von-Guericke University     http://www.cs.uni-magdeburg.de/
Department of Computer Science   Geb. 29 R 027, Universitaetsplatz 2
39106 Magdeburg, Germany         Tel: +49 391 67 52768


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

* Re: get keys of an associative array?
  2017-03-26 20:03   ` Bart Schaefer
@ 2017-03-26 22:06     ` Jens Elkner
  2017-03-27  8:36       ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Jens Elkner @ 2017-03-26 22:06 UTC (permalink / raw)
  To: zsh-workers

On Sun, Mar 26, 2017 at 01:03:31PM -0700, Bart Schaefer wrote:
> On Mar 26, 2017 12:41 PM, "Peter Stephenson" <p.w.stephenson@ntlworld.com>
> wrote:
> 
> You need "${(k)A[@]}" in ksh mode, but I'm not sure why the (@) flag
> doesn't do the trick.
> 
> The @ flag only makes things that are already arrays remain arrays in
> contexts where they would otherwise be joined.  In ksharrays context, ${A}
> is a scalar.

You mean man page section for the k flag needs enhancements? At least A
is explictly declared as an associative array, so I would expect, that
"${(k)A}" should work as well ...

Thanx,
jel.
-- 
Otto-von-Guericke University     http://www.cs.uni-magdeburg.de/
Department of Computer Science   Geb. 29 R 027, Universitaetsplatz 2
39106 Magdeburg, Germany         Tel: +49 391 67 52768


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

* Re: get keys of an associative array?
  2017-03-26 22:06     ` Jens Elkner
@ 2017-03-27  8:36       ` Peter Stephenson
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2017-03-27  8:36 UTC (permalink / raw)
  To: zsh-workers

On Mon, 27 Mar 2017 00:06:28 +0200
Jens Elkner <jel+zsh@cs.uni-magdeburg.de> wrote:
> On Sun, Mar 26, 2017 at 01:03:31PM -0700, Bart Schaefer wrote:
> > On Mar 26, 2017 12:41 PM, "Peter Stephenson" <p.w.stephenson@ntlworld.com>
> > wrote:
> > 
> > You need "${(k)A[@]}" in ksh mode, but I'm not sure why the (@) flag
> > doesn't do the trick.
> > 
> > The @ flag only makes things that are already arrays remain arrays in
> > contexts where they would otherwise be joined.  In ksharrays context, ${A}
> > is a scalar.
> 
> You mean man page section for the k flag needs enhancements? At least A
> is explictly declared as an associative array, so I would expect, that
> "${(k)A}" should work as well ...

I'm not sure how general an update this ought to be...

pws

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index e049aa5..dc6c494 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -1049,7 +1049,9 @@ If var(name) refers to an associative array, substitute the em(keys)
 (element names) rather than the values of the elements.  Used with
 subscripts (including ordinary arrays), force indices or keys to be
 substituted even if the subscript form refers to values.  However,
-this flag may not be combined with subscript ranges.
+this flag may not be combined with subscript ranges.  With the
+tt(KSH_ARRAYS) option a subscript `tt([*)' or `tt([@])' is needed
+to operate on the whole array, as usual.
 )
 item(tt(L))(
 Convert all letters in the result to lower case.


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

end of thread, other threads:[~2017-03-27  8:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-26 19:18 get keys of an associative array? Jens Elkner
2017-03-26 19:34 ` Peter Stephenson
2017-03-26 20:03   ` Bart Schaefer
2017-03-26 22:06     ` Jens Elkner
2017-03-27  8:36       ` Peter Stephenson
2017-03-26 21:55   ` Jens Elkner

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).