Mario DeFazio wrote:
> 
> I probably should know this, but is there a way to
> make a copy of array A to array B without writing a loop construct?
> 
> I have tried initializing B[0]=
> and then doing
> B=${A}
> or
> B=${A[*])
> 
> but I'm not getting the desired results.
> If each element of A is a single word, then
> set -A B  ${A[*]}
> 
> works, but that's rather limited.
> 
> At the moment I just want to copy a simple numeric indexed array
> but I'd also like to know how to do an associative array, too.

Technically something like $ typeset -a ar=( "${arr...@]}" ) # works for
non-sparse indexed arrays but it does not make 1:1 copies nor does it
work for sparse indexed or associative arrays...

AFAIK one way which works reliable for all types of variables is to wrap
the array in a compound variable and use the "copy" operator
("dest=src"), e.g.
-- snip --
$ ksh93 -c 'compound x=( typeset -a ar=( 1 2 3 ) ) ; compound y ; y=x ;
print -v y'                                   
(
        typeset -a ar=(
                1
                2
                3
        )
)
-- snip --

David: Would it be possible to allow "dest=src" to be used for arrays,
e.g. that this works (assuming both "src" and "dest" are arrays of the
same type (e.g. doing copies between "integer -a src" and something like
"compound -a dest" should result in an error)):
-- snip --
integer -a src=( 1 2 3 )
# ...
integer -a dest

dest=src # copy array
-- snip --

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to