On Wednesday, November 28, 2012 03:33:50 PM Clark WANG 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]=()'.
It seems to me that compound assignment to anything beyond a simple one-
dimensional array is completely random. I can't explain why anything does what
it does, and I've been working with these for a long time.
$ ksh -xs <<\EOF
namespace main {
typeset -a a[5]=() b[5][0]=() 'c[5][0]' 'd[5]' e=([5]=())
typeset -p .main {a..e}
print -C {a..e}
}
EOF
+ typeset -a a b 'c[5][0]' 'd[5]' e
+ typeset -p .main a b c d e
namespace main
{
typeset -a a=([0]=)
typeset -a b=([5]=(;) ) )
typeset -a d=('')
typeset -a e=([5]=())
}
typeset -a a=([0]=)
typeset -a b=([5]=(;) ) )
typeset -a c=(() )
typeset -a d=('' '' '' '' '')
typeset -a e=([5]=())
+ print -C a b c d e
([0]=)
([5]=(;) ) )
(() () () () () )
('' '' '' '' '')
([5]=())
I assume you expected something like "e", except if you check the type of
e[5], it's actually compound. Matters get a little better if you don't specify
an indexed array, you at least get the correct elements, but then the types
aren't necessarily correct.
ksh -xc <<\EOF
namespace main {
typeset a[5]=() b[5][0]=() 'c[5][0]' 'd[5]' e=([5]=())
typeset -p .main {a..e}
print -C {a..e}
}
+ typeset a b 'c[5][0]' 'd[5]' e
+ typeset -p .main a b c d e
namespace main
{
typeset -a a=([5]=())
typeset -a b=([5]=(()) )
typeset -a c=([5]=)
typeset -a d=()
typeset -A e=([5]=())
}
typeset -a a=([5]=())
typeset -a b=([5]=(()) )
typeset -a c=([5]=)
typeset -a d=()
typeset -A e=([5]=())
+ print -C a b c d e
([5]=())
([5]=(()) )
([5]=)
([5]=())
--
Dan Douglas
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users