A workaround may be use send compound variables via pipe:
ksh93 -c '( for ((i=0 ; i < 10 ; i++ )) ; do compound x ; integer
x.val=i ; print -v x ; done ) | ( compound r ; while read -C r ; do
printf "%s\n" ${r.val} ; done )'
0
1
2
3
4
5
6
7
8
9
The compound variable may contain arrays or array elements, too.
Olga
On Fri, Mar 19, 2010 at 10:38 PM, Cyrille Lefevre
<[email protected]> wrote:
>
> [email protected] a écrit :
>>
>> AST-Users,
>>
>> I had written a function essentially like "loadTheArrayVer2" below. In
>> the calling code, after calling the function, my array was dead empty. To my
>> way of thinking, this behavior is most unexpected, If this is proper, then
>> please help me understand the logic behind it, so that I don't make the same
>> mistake again.
>>
>> $ cat kshScopeIssue
>> function loadTheArrayVer1
>> {
>> nameref array_ref=$1
>> print | for i in justOnce; do array_ref[jdoe]='John Doe'; done
>> }
>> function loadTheArrayVer2
>> {
>> nameref array_ref=$1
>> print | for i in justOnce; do array_ref[jdoe]='John Doe'; done | while
>> read; do :; done
>> }
>> function loadTheArrayVer3
>> {
>> nameref array_ref=$1
>> for i in justOnce; do array_ref[jdoe]='John Doe'; done | while read; do
>> :; done
>> }
>
> <snip>
>>
>> $ ./kshScopeIssue Version JM 93t+ 2010-02-02
>> Using loadTheArrayVer1: John Doe
>> Using loadTheArrayVer2:
>> Using loadTheArrayVer3:
>
> only the last sequence of a pipe may be executed in the current shell,
> the others sequences are executed in a subshell, so variable assignments are
> lost in there.
>
> the portable way to keep assignments w/in loop is as follow :
>
> function loadTheArrayVer0
> {
> nameref array_ref=$1
> for i in justOnce; do array_ref[jdoe]='John Doe'; done << EOF
> $(print)
> EOF
> }
>
> man /opt/ast/man/man1/sh.1
> A pipeline is a sequence of one or more commands separated by |. The
> standard output of each command but the last is connected by a pipe(2)
> to the standard input of the next command. Each command, except possi-
> bly the last, is run as a separate process; the shell waits for the
> last command to terminate. The exit status of a pipeline is the exit
> status of the last command unless the pipefail option is enabled. Each
> pipeline can be preceded by the reserved word ! which causes the exit
> status of the pipeline to become 0 if the exit status of the last com-
> mand is non-zero, and 1 if the exit status of the last command is 0.
>
> take care, in bash or pdksh, all sequences of a pipe are executed in a
> subshell !
>
> man bash
> Each command in a pipeline is executed as a separate process (i.e., in
> a subshell).
>
> ksh88
> $ ^Version 11/16/88f
> $ i=1 | j=1
> $ echo $i:$j
> :1
>
> ksh93
> $ echo ${.sh.version}
> Version JM 93t+ 2009-05-01
> ditto
>
> bash
> $ echo $BASH_VERSION
> 3.2.49(23)-release
> $ i=1 | j=1
> $ echo $i:$j
> :
>
> pdksh
> $ echo $KSH_VERSION
> @(#)PD KSH v5.2.14 99/07/13.2
> ditto
>
> Regards,
>
> Cyrille Lefevre
> --
> mailto:[email protected]
>
>
>
> _______________________________________________
> ast-users mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-users
>
--
, _ _ ,
{ \/`o;====- Olga Kryzhanovska -====;o`\/ }
.----'-/`-/ [email protected] \-`\-'----.
`'-..-| / Solaris/BSD//C/C++ programmer \ |-..-'`
/\/\ /\/\
`--` `--`
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users