I've been doing some work with compound arrays lately, and I've run
into some confusing issues. The command sequence below illustrates:

$ # Given the following:
$
$ typeset -a array1=(a b c)
$ typeset -a array2=((a b c) (d e f) (g h i))
$ typeset -A array3=([a]=(1 2 3) [b]=(4 5 6) [c]=(7 8 9))
$
$ # The following operations generally make sense:
$ set -x
$
$ array1+=d
+ array1[0]+=d
$ typeset -p array1
+ typeset -p array1
typeset -a array1=(ad b c)
$
$ array1+=(e)
+ array1+=( e )
$ typeset -p array1
+ typeset -p array1
typeset -a array1=(ad b c e)
$
$ array3+=([d]=(10 11 12))
+ array3[d]=( 10 11 12 )
$ typeset -p array3
+ typeset -p array3
typeset -A array3=([a]=(1 2 3) [b]=(4 5 6) [c]=(7 8 9) [d]=(10 11 12) )
$
$ # But, the operations below don't follow from what I see above:
$
$ array2+=(j k l)
+ array2+=( j k l )
$ typeset -p array2
+ typeset -p array2
typeset -a array2=((a b c) (d e f) (g h i) j k l)
$
$ # From array1 above I would expect
$ # array2=((a b c j k l) (d e f) (g h i) ).
$ # I suppose it might make sense if array2 is viewed as a list of
$ # lists.
$
$ # Resetting array2.
$ typeset -a array2=((a b c) (d e f) (g h i))
+ array2[0]=( a b c )
+ array2[1]=( d e f )
+ array2[2]=( g h i )
+ typeset -a array2
$
$ # This next one is clearly wrong.
$
$ array2+=((j k l))
+ array2[0]=( j k l )
$ typeset -p array2
+ typeset -p array2
typeset -a array2=((j k l) )
$
$ # The += operation should not be destructive.
$
$ # Reset again.
$ typeset -a array2=((a b c) (d e f) (g h i))
+ array2[0]=( a b c )
+ array2[1]=( d e f )
+ array2[2]=( g h i )
+ typeset -a array2
$
$ # Fortunately there is a way to append an indexed array to the end of
$ # a compound indexed array:
$
$ array2[${#array2[@]}]=(j k l)
+ array2[3]=( j k l )
$ typeset -p array2
+ typeset -p array2
typeset -a array2=((a b c) (d e f) (g h i) (j k l) )
$
$ # But, that's much more cumbersome than the += operator.
$ set +x
$
$ # Here's some more strange behavior.
$ # Shouldn't the next two print statements produce the same output?
$
$ print ${array2[@]}
a d g j
$ print ${array2[0..3]}
a b c
$
$ # It appears to work with array3:
$
$ print ${array3[@]}
1 4 7 10
$ print ${array3[a..d]}
1 4 7 10
$
$ # And, if these work (sort of):
$
$ print ${array2[0-3][0-3]}
d
$ print ${array3[a..d][0..2]}
1
$ print ${array3[0..2][a..d]}
1 4 7 10
$
$ # Why are these errors?
$
$ print ${array2[@][@]}
ksh: @: arithmetic syntax error
$ print ${array3[@][@]}
ksh: ${array3[@][@]}: bad substitution
$
$ # I suppose the following is rather roundabout, but why does the ../:0
$ # combination sometimes drop elements?
$
$ print ${array2[@]:0}          # OK
a d g j
$ print ${array2[0..3]:0}       # OK, but again, different from @
a b c
$ print ${array2[0..2]}
a b c
$ print ${array2[0..2]:0}       # c dropped
a b
$ print ${array3[@]:0}          # OK
1 4 7 10
$ print ${array3[a..d]:0}       # 4, 7, 10 dropped
1
$

I could probably come up with some more confusing combinations, but
that's enough for now.

                    Terrence Doyle
_______________________________________________
ast-users mailing list
ast-users@lists.research.att.com
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to