"U.V. Ravindra" wrote:
> > "U.V. Ravindra" wrote:
> > > The final for-loop in the script I supplied to
> > Roland is obviously mis-written.
> > > It should be:
> > >
> > > typeset -i idx=0
> > >
> > > for arg in ${argtype[*]}; do
> > > print "${argtype[$idx]} \t ${argname[$idx]}
> > \t ${argvalue[$idx]}"
> > > ((idx = idx + 1))
> > > done
> >
> > Erm... shouldn't this for-loop iterate over each
> > array element ? If
> > "yes" the loop should AFAIK look like this:
> > -- snip --
> > integer idx
> >
> > for (( idx=0 ; idx < ${!argty...@]} ; idx++ )) ; do
> > printf "%s \t %s \t %s\n" \
> > "${argtype[$idx]}" \
> > "${argname[$idx]}" \
> > "${argvalue[$idx]}"
> > snip --
>
> The answer to your question is "yes", but the loop I
> provided does the same thing; it iterates over each
> array element. Sure, we could do without introducing
> the extra 'arg' variable, but that's not the point
> you're making :)
The problem with the original code is that it used the [*] subscript -
which means the whole array is expanded as _one_ single string
(http://www.opensolaris.org/os/project/shell/shellstyle/#store_lists_in_arrays
has a note about the difference between [...@] and [*]), seperated by the
first character in IFS. AFAIK there are two "correct" options:
1. Use "${argty...@]}" - it will expand each element in "argtype" within
double-quotes seperately (which means it will work even if there are
whitespaces (e.g. space, tab, newline) involved)
2. Use "${!argty...@]}" to return the list of array subscripts (e.g. the
index numbers for each array element)
> By the way, the "wrong" loop in the script originally
> supplied ALSO produces the expected output, but that
> should be considered another bug in ksh93.
> When idx is explicitly declared as integer-typed and
> argtype is an array containing strings, I would
> expect the following to fail:
>
> typeset -i idx=0
>
> for idx in ${argtype[*]}; do
> print ${argtype[$idx]}
> done
Erm... why should this be an error ? The array uses integers as _index_
but this is independent from the datatype used by the _values_ of an
array element.
For example:
$ float -a foo # ("float" is an alias for "typeset -El") creates an
normal array (integers used as index) with floating point as type for
the array elements
or
$ typeset -a -i myarray # creates the array "myarray" and uses integers
as type for the array elements.
Just using "typeset -a" (or the set -A thing) defines an arary which
uses integers as index and "string" as type for the array elements.
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL <currently fluctuating>
(;O/ \/ \O;)