On Thu, Nov 29, 2012 at 8:56 PM, Dan Douglas <[email protected]> wrote:
> 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]=())

Erm... what exactly do you want to do in this case ? The example above
contains a wild mixture of everything... including things which are
corner-cases... like adding compound variables to a plain string array
(which is AFAIK only allowed for legacy purposes since now we have
typeset -C to explicitly declare an array of compound variables) and
mixing dimensions in a multi-dimensional array (AFAIK
multi-dimensional arrays should only have one number of dimensions and
not multiple numbers of dimensions).

----

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

Reply via email to