bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Arithmetic assignment side-effects


From: Dan Douglas
Subject: Re: Arithmetic assignment side-effects
Date: Mon, 05 Aug 2013 00:36:09 -0500
User-agent: KMail/4.10.5 (Linux/3.10.1-pf+; KDE/4.10.5; x86_64; ; )

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]