On Sunday, August 04, 2013 06:08:18 PM Linda Walsh wrote:
> From the bash manpage, it would see that += is higher precedence
> than assignment, so the increment would be done first, followed
> by the attempt at an assignment of 1 to 1.
= and += have equal precedence. Associativity is right to left, as described in
operator(7). x=1 occurs first.
granular debugging is hard. For ((x[a] += x[b] = 1)):
output:
---
referenced x[a] with value 0
x[b]: 0 -> 1
referenced .sh.x[b] with value 1
x[a]: 1 -> 1
referenced .sh.x[a] with value 1
result: 1
.sh.value isn't incrementing here like i'd expect. A better test would be to
use a compound with namerefs pointing at a single variable, but ksh provides no
way to tell which ref is being set. Other shells have no way to hook a set,
though any shell with arrays and recursive integer vars makes it easy too hook
gets.
---
#!/usr/bin/env ksh
typeset -A x=([a]=0; [b]=0)
integer i=0
function x.get {
print -r "referenced ${.sh.name}[${.sh.subscript}] with value $i"
((.sh.value = i))
}
function x.set {
printf "%s: %d -> %d\n" "${.sh.name}[${.sh.subscript}]" $i ${.sh.value}
((i = .sh.value))
}
((x[a] += x[b] = 1))
print result: $i
--
Dan Douglas