I have a question regarding the use of an indexed array of typed
variables. When I declare such an array, and use the += operator to
add a value, the first index is "1", rather than "0". Is this the
intended behavior? e.g.:

========================================
$ echo ${.sh.version}
Version AIJM 93v- 2013-10-08
$ typeset -T Foo_t=(integer x y)
$ Foo_t -a foo
$ foo+=(x=111 y=222)
$ foo+=(x=333 y=444)
$ echo ${!foo[@]}
1 2
$ integer -a bar
$ bar+=(111)
$ bar+=(222)
$ echo ${!bar[@]}
0 1
========================================

The two arrays have the same number of elements, but,
counterintuitively, different index values, which one has to be
cognizant of when accessing the elements of these arrays.

I would like to be able to set the values of such arrays in a loop; e.g.:
========================================
typeset -T Foo_t=(integer x y)
Foo_t -a foo
integer -a bar

while read -r line
do
...
  foo+=(x=${someval1} y=${someval2})
  bar+=(${someval3})
...
done
========================================

However, using this method, they require different loop indexes:

========================================
for (( i = 0; i < ${#bar[@]}; i++ ))
do
...

for (( i = 1; i < ${#foo[@]}; i++ ))
do
...
========================================

A less-elegant way of setting the "foo" array in a loop so that it is
0-indexed is:

========================================
  foo[${#foo[@]}]=(x=${someval1} y=${someval2))
========================================

However, it if the current behavior of "+=" with respect to indexed
arrays of typed variables isn't well-known or relied upon, it might be a
good candidate to be changed for consistency with other indexed arrays
in a future release.

--
Nathan Weeks
IT Specialist
USDA-ARS Corn Insects and Crop Genetics Research Unit
http://weeks.public.iastate.edu/
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to