On Wed, Nov 28, 2012 at 8:33 AM, Clark WANG <[email protected]> wrote:
> I was trying to find a way to safely initialize arr[n] to an empty indexed
> array and I tried this:
>
> $ cat foo.ksh
> echo ${.sh.version}
>
> typeset -a EMPTY_ARRAY=()
> typeset -a g_arr=()
>
> function initialize
> {
> g_arr[0]=(11 22 33)
> g_arr[1]=( "${EMPTY_ARRAY[@]}" )
> }
>
> initialize
> typeset -p g_arr[0]
> typeset -p g_arr[1]
> $ ksh foo.ksh
> Version AJMP 93u+ 2012-08-01
> typeset -a g_arr[0]=(11 22 33)
> $
>
> From the result we can see g_arr[1] was not set at all which I think is not
> correct. At least it should behave the same as `g_arr[1]=()'.
Erm... this should work:
1. Add content of an empty string array to an existing string array:
-- snip --
$ ksh -c 'typeset -a ar=( a b c ) ; typeset -a empty=( ) ; ar+=(
"${empty[@]}" ) ; print -v ar'
(
a
b
c
)
-- snip --
2. Same demo... but "empty" now has one array element:
-- snip --
$ ksh -c 'typeset -a ar=( a b c ) ; typeset -a empty=( 4 ) ; ar+=(
"${empty[@]}" ) ; print -v ar'
(
a
b
c
4
)
-- snip --
3. The same works with compound variables... using the variable _name_
(not value):
-- snip --
$ ksh -c 'compound -a ar=( (a=1) (b=2) (c=3) ) ; compound -a empty=(
(d=4) ) ; ar+=( empty[0] ) ; print -v ar'
(
(
a=1
)
(
b=2
)
(
c=3
)
(
d=4
)
)
-- snip --
4. More complex example for compound variable arrays, using the
"${!var[@]}"-operator to list the index names of an array and POSIX
printf(1) ability to repeat the printf format string each time when
there are arguments left over:
-- snip --
$ ksh -c 'compound -a ar1=( (a=1) (b=2) (c=3) ) ; compound -a ar2=(
(d=4) (e=5) ) ; ar1+=( $(printf "ar2[%s]\n" "${!ar2[@]}") ) ; print -v
ar1'
(
(
a=1
)
(
b=2
)
(
c=3
)
(
d=4
)
(
e=5
)
)
-- 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]
http://lists.research.att.com/mailman/listinfo/ast-users