zsh-users
 help / color / mirror / code / Atom feed
* Copy assiociative arrays
@ 2012-03-21 21:28 René Neumann
  2012-03-21 21:44 ` Mikael Magnusson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: René Neumann @ 2012-03-21 21:28 UTC (permalink / raw)
  To: zsh-users

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

Hi all,

is it possible to copy associative arrays?

I.e. have

declare -A foo bar
foo=(muh vier bla "\\sechs")
bar=$foo

using this, bar only contains the values of foo (and using ${(kv)foo}
instead still only makes it a string).

The current awkward way I found is:

declare -A foo bar
foo=(muh vier bla "\\sechs")
: ${(AA)bar::=${(kv)foo}}

But this seems rather cluttered and unintuitive...

The other way is using indirection, in that bar only holds the string
"foo" and with the (P) flag this is resolved. But unfortunately I can't
make it work with subscripts:

declare -A foo bar
foo=(muh vier bla "\\sechs")
bar=foo
# all output the empty string
echo ${${(P)bar}[muh]}
echo ${${bar}[muh]}

Thanks and regards,
René


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

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

* Re: Copy assiociative arrays
  2012-03-21 21:28 Copy assiociative arrays René Neumann
@ 2012-03-21 21:44 ` Mikael Magnusson
  2012-03-21 21:46 ` Moritz Bunkus
  2012-03-21 22:38 ` Bart Schaefer
  2 siblings, 0 replies; 6+ messages in thread
From: Mikael Magnusson @ 2012-03-21 21:44 UTC (permalink / raw)
  To: René Neumann; +Cc: zsh-users

On 21 March 2012 22:28, René Neumann <lists@necoro.eu> wrote:
> Hi all,
>
> is it possible to copy associative arrays?
>
> I.e. have
>
> declare -A foo bar
> foo=(muh vier bla "\\sechs")
> bar=$foo
>
> using this, bar only contains the values of foo (and using ${(kv)foo}
> instead still only makes it a string).

You still need the () when assigning to an array, even if the values
come from an array expansion
bar=( ${(kv)foo} )

-- 
Mikael Magnusson


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

* Re: Copy assiociative arrays
  2012-03-21 21:28 Copy assiociative arrays René Neumann
  2012-03-21 21:44 ` Mikael Magnusson
@ 2012-03-21 21:46 ` Moritz Bunkus
  2012-03-21 22:08   ` René Neumann
  2012-03-21 22:38 ` Bart Schaefer
  2 siblings, 1 reply; 6+ messages in thread
From: Moritz Bunkus @ 2012-03-21 21:46 UTC (permalink / raw)
  To: René Neumann; +Cc: zsh-users

Hey,

what about

$ declare -A foo bar
$ foo=(a 42 b 54)
$ bar=(${(kv)foo})
$ print ${(kv)bar}
a 42 b 54

> using this, bar only contains the values of foo (and using ${(kv)foo}
> instead still only makes it a string).

Yes, because you didn't put it into parenthesis, bar=${(kv)foo} vs
bar=(${(kv)foo})

Kind regards,
mo


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

* Re: Copy assiociative arrays
  2012-03-21 21:46 ` Moritz Bunkus
@ 2012-03-21 22:08   ` René Neumann
  2012-03-21 22:15     ` Frank Terbeck
  0 siblings, 1 reply; 6+ messages in thread
From: René Neumann @ 2012-03-21 22:08 UTC (permalink / raw)
  To: zsh-users

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

Am 21.03.2012 22:46, schrieb Moritz Bunkus:
> Hey,
> 
> what about
> 
> $ declare -A foo bar
> $ foo=(a 42 b 54)
> $ bar=(${(kv)foo})
> $ print ${(kv)bar}
> a 42 b 54
> 
>> using this, bar only contains the values of foo (and using ${(kv)foo}
>> instead still only makes it a string).
> 
> Yes, because you didn't put it into parenthesis, bar=${(kv)foo} vs
> bar=(${(kv)foo})

This makes sense. Thanks for the pointer :)

- René



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

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

* Re: Copy assiociative arrays
  2012-03-21 22:08   ` René Neumann
@ 2012-03-21 22:15     ` Frank Terbeck
  0 siblings, 0 replies; 6+ messages in thread
From: Frank Terbeck @ 2012-03-21 22:15 UTC (permalink / raw)
  To: René Neumann; +Cc: zsh-users

René Neumann wrote:
> Am 21.03.2012 22:46, schrieb Moritz Bunkus:
>> Hey,
>> 
>> what about
>> 
>> $ declare -A foo bar
>> $ foo=(a 42 b 54)
>> $ bar=(${(kv)foo})
>> $ print ${(kv)bar}
>> a 42 b 54
>> 
>>> using this, bar only contains the values of foo (and using ${(kv)foo}
>>> instead still only makes it a string).
>> 
>> Yes, because you didn't put it into parenthesis, bar=${(kv)foo} vs
>> bar=(${(kv)foo})
>
> This makes sense. Thanks for the pointer :)

Almost. If one of the values in the source hash is empty, this will
fail:

[snip]
zsh% typeset -A bar
zsh% typeset -A foo
zsh% foo=( a foo b bar c "" )
zsh% bar=( ${(kv)foo} )
zsh: bad set of key/value pairs for associative array
[snap]

To cover that situation you got two options, which are equivalent:

  % bar=( "${(kv@)foo}" )
  % bar=( "${(kv)foo[@]}" )

Whichever you like better.

Regards, Frank


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

* Re: Copy assiociative arrays
  2012-03-21 21:28 Copy assiociative arrays René Neumann
  2012-03-21 21:44 ` Mikael Magnusson
  2012-03-21 21:46 ` Moritz Bunkus
@ 2012-03-21 22:38 ` Bart Schaefer
  2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2012-03-21 22:38 UTC (permalink / raw)
  To: zsh-users

On Mar 21, 10:28pm, Rene Neumann wrote:
}
} declare -A foo bar
} foo=(muh vier bla "\\sechs")
} bar=foo

That assignment converts bar into a scalar.

} # all output the empty string
} echo ${${(P)bar}[muh]}

An aside to explain this ...

${(P)bar} == ${foo} which is the array of values in the hash foo, in
no particular order.  So it could be that ${${(P)bar}[2]} == "vier",
but the hash keys have been discarded by the time the subscript is
evaluated.

You can indirect into a hash like so:

bar='foo[muh]'
print ${(P)bar}

Unfortunately there's no subscript notation that returns both the key
and the value for an associative array element, and there's no way to
embed the (kv) expansion flag in the indirection.  Zsh doesn't yet have
true "nameref" semantics like ksh.


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

end of thread, other threads:[~2012-03-21 22:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-21 21:28 Copy assiociative arrays René Neumann
2012-03-21 21:44 ` Mikael Magnusson
2012-03-21 21:46 ` Moritz Bunkus
2012-03-21 22:08   ` René Neumann
2012-03-21 22:15     ` Frank Terbeck
2012-03-21 22:38 ` 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).