Discussion:
[ast-developers] Problem with evaluating arithmetric expressions in nameref names passed as function argument...
Roland Mainz
2013-05-27 13:08:29 UTC
Permalink
Hi!

----

The following example...
-- snip --
$ ksh -c 'typeset -a ar=( 2 3 4 ) ; function fn { nameref n=$1 ; print
-- "$n" ; } ; function main { integer i ; for ((i=0 ; i < ${#ar[@]} ;
)) ; do fn ar[i++] ; done ; } ; main ; true'
-- snip --
... should AFAIK print...
--- snip --
2
3
4
-- snip --
... but ast-ksh.2013-05-24 runs in an endless loop (warning (mostly
for myself): Any test module should have a 2nd integer aborting the
endless loop) printing newlines...

The workaround is to replace the ar[i++] with ar[$((i++))], e.g. ...
-- snip --
$ ksh -c 'typeset -a ar=( 2 3 4 ) ; function fn { nameref n=$1 ; print
-- "$n" ; } ; function main { integer i ; for ((i=0 ; i < ${#ar[@]} ;
)) ; do fn ar[$((i++))] ; done ; } ; main ; true'
2
3
4
-- snip --
... so that the nameref only gets a "static" string as index... the
only issue is that this requires an internal integer-to-string
conversion which is IMO unneccesary...

----

Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
Roland Mainz
2013-05-27 13:20:20 UTC
Permalink
Post by Roland Mainz
The following example...
-- snip --
$ ksh -c 'typeset -a ar=( 2 3 4 ) ; function fn { nameref n=$1 ; print
)) ; do fn ar[i++] ; done ; } ; main ; true'
-- snip --
... should AFAIK print...
--- snip --
2
3
4
-- snip --
... but ast-ksh.2013-05-24 runs in an endless loop (warning (mostly
for myself): Any test module should have a 2nd integer aborting the
endless loop) printing newlines...
The workaround is to replace the ar[i++] with ar[$((i++))], e.g. ...
-- snip --
$ ksh -c 'typeset -a ar=( 2 3 4 ) ; function fn { nameref n=$1 ; print
)) ; do fn ar[$((i++))] ; done ; } ; main ; true'
2
3
4
-- snip --
... so that the nameref only gets a "static" string as index... the
only issue is that this requires an internal integer-to-string
conversion which is IMO unneccesary...
Uhm... Ok... here is a related issue... it seems the arithmetric
expression is exected in the wrong scope:
-- snip --
$ ksh -o nounset -c 'typeset -a ar=( 2 3 4 ) ; function fn { nameref
n=$1 ; print -- "$n" ; } ; function main { integer i ; for ((i=0 ; i <
${#ar[@]} ; )) ; do fn ar[i++] ; done ; } ; main ; true' 2>&1 | head
-2
/home/test001/bin/ksh: main[1]: fn[1]: typeset: i: parameter not set
/home/test001/bin/ksh: main[1]: fn[1]: typeset: i: parameter not set
-- snip --
... AFAIK the issue is that the arithmetic expression should be
executed in the scope of the calling function... or we simply say that
this is not allowed and names passed to namerefs must not contain any
arithmetic expressions...

----

Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz at nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
Loading...