On Thu, Jan 24, 2013 at 10:05 PM, David Korn <[email protected]> wrote:
> cc: [email protected]
> Subject: Re: [ast-users] nameref confusion--not expanding as expected?
> --------
>
> This is caused by a for loop optimizer bug.  It treated $v as a loop
> invariant.  I am looking for a solution.

I'm not sure whether it's a bug at all... the issue is that he uses
"nameref v" to declare a nameref but doesn't give the nameref a
variable name as input. Technically the first assignment of a value to
a nameref defines the name of the variable it will access while
following accesses set the value of the alias'ed variable.

Changing Aaron's example to this version fixes the problems:
-- snip --
set -e
set -u

print ${.sh.version}

function dumpParams # array
{
    set -eu
    nameref a=$1
    shift
    nameref v
    print '--- works'
    for v in "${a[@]}"
    do
        print "${!v}=$v"
    done
    print '---'
}

function dumpParams2 # array
{
    set -eu
    nameref a=$1
    shift

    print '--- doesn'\''t work'
    for vn in "${a[@]}"
    do
        nameref v=$vn
        print "${!v}" = "$v"
    done
    print '---'
}

foo=1
bar=2
baz=3
array=(foo bar baz)

dumpParams  array
print
dumpParams2 array
-- snip --

Unified diff looks like this:
-- snip --
--- aaron_nameref_original.sh        2013-01-24 22:32:28.122995107 +0100
+++ aaron_nameref_working.sh        2013-01-24 22:32:14.169997998 +0100
@@ -22,10 +22,11 @@
     set -eu
     nameref a=$1
     shift
-    nameref v
+
     print '--- doesn'\''t work'
-    for v in "${a[@]}"
+    for vn in "${a[@]}"
     do
+       nameref v=$vn
         print "${!v}" = "$v"
     done
     print '---'
-- snip --

The fix is simply to re-declare the nameref each time it should alias
a new variable (or array element).

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to