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

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



2010-01-11 20:13:12 +0000, Stephane Chazelas:
[...]
> someone reported the exact same problem with bash. And it also
> occurs with zsh and ksh93.

Note that this bug has already been fixed in ksh93
(http://mailman.research.att.com/pipermail/ast-developers/2010q1/000550.html),
not in bash yet.

> ~$ unset x; for i ({1..1000}) ((x[RANDOM%2+1]++)); echo $x
> 508 509
> 
> (see how the sum is not 1000 and how the numbers are close
> together).
> 
> My understanding is that
> 
> x[RANDOM%2+1]++
> 
> is handled as
> 
> x[RANDOM%2+1] = x[RANDOM%2] + 1
> 
> and the 2 RANDOMs above are expanded twice, so will generally be
> different. But I would still say it's a bug.
> 
> Similar problem:
> 
> $ x=0; ((a[++x]++)); echo $x
> 2
> 
> All the +=, -=, *=... operators are also affected.

-- 
Stephane



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