Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: subscript expanded twice in a[subcript]++ (Was: problem with RANDOM and arrays)



On Jan 20,  4:46pm, Peter Stephenson wrote:
}
} Had an idea...  the second level down in the parameter code contains the
} evaluated subscript, so we can cache that, and be as paranoid as we feel
} about the validity of the cached value.

Much better:

schaefer<503> x=0; a=(x+=3 x+=2 x+=1 x+=4 x+=5); ((++a[++x])); print $a $x
5 x+=2 x+=1 x+=4 x+=5 4

Still a little dicey, but probably not fixable:

schaefer<507> x=3; a=(x+=3 x+=2 x+=1 x+=4 x+=5); ((++a[++a[1]])); print $a $x
7 x+=2 x+=1 x+=4 x+=5 1 6

In that expression, we read a[1] and get (x+=3) so we increment a[1] as
(x+=3+1)=7 and then increment a[7] (which was previously unset) to get 1.

There's probably some really complicated side-effect threading you could
get going with this.



Messages sorted by: Reverse Date, Date, Thread, Author