Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: copying an array



Hi,

Try this:

eval $1'=("${G[@]}")'

If the value of $1 is foo, this is substituted with eval foo=("${G[@]}") and
evaluate it.

regards

From: Dominik Vogt <vogt@xxxxxxxxxxxxxxxxxx>
Subject: copying an array
Date: Tue, 5 Aug 2014 06:44:34 +0100

> I'm trying to write a function that
> 
>   * takes the name of a new array variable as its argument,
>   * creates a global array variable with the given name
>   * gets a copy of the values of another global array.
> 
> Sketch of the function:
> --
> typeset -g -a G
> G=("a b c" d e)
> copy_array () {
> 	typeset -g -a "$1"
> 	# how can this be done?
> 	$1="($G[@])"
> }
> --
> 
> The function needs to handle whitespace (or any special
> characters) in the values of G gracefully.  The closest I've got so
> far is
> 
>   eval $(echo "$1=($G[@])")
> 
> But this fails to handle whitespace properly.
> 
> Bonus points if this can be done without eval.  :-)
> 
> Any ideas?
> 
> Ciao
> 
> Dominik ^_^  ^_^
> 
> 



Messages sorted by: Reverse Date, Date, Thread, Author