Cyrille, 

Thank you for the thorough explanation. I've been aware of that behavior, but I 
hadn't previously used nameref variables. I was thinking of the nameref as 
giving me direct, sort-of-real-time, no-intermediaries-involved, access to that 
associative array. So, it seemed reasonable that as soon as the assignment was 
completed, from wherever in that function, it would actually modify the array 
value. Isn't that a reasonable expectation for the pass-by-reference variable? 

Thanks, 
Dan 


----- Original Message ----- 
From: "Cyrille Lefevre" <[email protected]> 
To: "dan rickhoff" <[email protected]> 
Cc: [email protected] 
Sent: Friday, March 19, 2010 2:38:18 PM GMT -08:00 US/Canada Pacific 
Subject: Re: [ast-users] Why doesn't my array assignment stick? 


[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

Reply via email to