zsh-workers
 help / color / mirror / code / Atom feed
* clone an associative array?
@ 2005-08-19 14:13 Marc Chantreux
  2005-08-19 14:24 ` Peter Stephenson
  2005-08-19 14:25 ` Stephane Chazelas
  0 siblings, 2 replies; 5+ messages in thread
From: Marc Chantreux @ 2005-08-19 14:13 UTC (permalink / raw)
  To: zsh-workers

hello all,

i don't know what i missed in 'parameters expansions' manual but i'm
unable to clone an associative array :

% typeset -A homemade
% homemade=( tm myself c 2005 )
% echo $homemade[tm]
myself

% typeset -A counterfeit
% counterfeit=${homemade}

% echo $counterfeit[c]
m

% counterfeit="${homemade}"
% echo $counterfeit[c]
m

% counterfeit="${(AA)homemade}"
% echo $counterfeit[c]
m

% counterfeit="${(A)homemade}"
% echo $counterfeit[c]
m

# other tries failed too ...

in a future time, i would like to write something as :

func () {
    local who=$1
    shift
    local -A config
    config=$@

    # implementation here ....

}

i'm sure that it is possible, i just want to know how.

regards
mc


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

* Re: clone an associative array?
  2005-08-19 14:13 clone an associative array? Marc Chantreux
@ 2005-08-19 14:24 ` Peter Stephenson
  2005-08-19 14:30   ` Peter Stephenson
  2005-08-19 14:30   ` Stephane Chazelas
  2005-08-19 14:25 ` Stephane Chazelas
  1 sibling, 2 replies; 5+ messages in thread
From: Peter Stephenson @ 2005-08-19 14:24 UTC (permalink / raw)
  To: zsh-workers

Marc Chantreux wrote:
> hello all,
> 
> i don't know what i missed in 'parameters expansions' manual but i'm
> unable to clone an associative array :
> 
> % typeset -A homemade
> % homemade=( tm myself c 2005 )
> % echo $homemade[tm]
> myself
> 
> % typeset -A counterfeit
> % counterfeit=${homemade}

As it's a sort of array, you need braces.  However, you also need
to copy keys as well as values.  The syntax is:

typeset -A counterfeit
counterfeit=(${(kv)homemade})

Make sure you don't have the option SH_WORD_SPLIT set.  (This is a
classic example of why it's a good idea to get used to doing without it.)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070




**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


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

* Re: clone an associative array?
  2005-08-19 14:13 clone an associative array? Marc Chantreux
  2005-08-19 14:24 ` Peter Stephenson
@ 2005-08-19 14:25 ` Stephane Chazelas
  1 sibling, 0 replies; 5+ messages in thread
From: Stephane Chazelas @ 2005-08-19 14:25 UTC (permalink / raw)
  To: Marc Chantreux; +Cc: zsh-workers

On Fri, Aug 19, 2005 at 04:13:38PM +0200, Marc Chantreux wrote:
[...]
> % typeset -A counterfeit
> % counterfeit=${homemade}
[...]

counterfeit=("${(@kv)homemade}")

-- 
Stephane


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

* Re: clone an associative array?
  2005-08-19 14:24 ` Peter Stephenson
@ 2005-08-19 14:30   ` Peter Stephenson
  2005-08-19 14:30   ` Stephane Chazelas
  1 sibling, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2005-08-19 14:30 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson wrote:
> > % typeset -A counterfeit
> > % counterfeit=${homemade}
> 
> As it's a sort of array, you need braces.

I meant parentheses, of course.  You've already got the braces.

> typeset -A counterfeit
> counterfeit=(${(kv)homemade})
> 
> Make sure you don't have the option SH_WORD_SPLIT set.  (This is a
> classic example of why it's a good idea to get used to doing without it.)

This is what Stephane's version works around.

pws


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


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

* Re: clone an associative array?
  2005-08-19 14:24 ` Peter Stephenson
  2005-08-19 14:30   ` Peter Stephenson
@ 2005-08-19 14:30   ` Stephane Chazelas
  1 sibling, 0 replies; 5+ messages in thread
From: Stephane Chazelas @ 2005-08-19 14:30 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On Fri, Aug 19, 2005 at 03:24:52PM +0100, Peter Stephenson wrote:
[...]
> typeset -A counterfeit
> counterfeit=(${(kv)homemade})
> 
> Make sure you don't have the option SH_WORD_SPLIT set.  (This is a
> classic example of why it's a good idea to get used to doing without it.)
[...]

Hmmm,

~$ typeset -A a b
~$ a=(a 1 b '' c 2)
~$ echo "$a[b]"

~$ b=(${(kv)a})
zsh: bad set of key/value pairs for associative array

Hence, the b=("${(@kv)a}") that would also work with
SH_WORD_SPLIT.

-- 
Stephane


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

end of thread, other threads:[~2005-08-19 14:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-19 14:13 clone an associative array? Marc Chantreux
2005-08-19 14:24 ` Peter Stephenson
2005-08-19 14:30   ` Peter Stephenson
2005-08-19 14:30   ` Stephane Chazelas
2005-08-19 14:25 ` Stephane Chazelas

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