[Top][All Lists]

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

Re: V+=1 doesn't work if V is a reference to an integer array element

From: Léa Gris
Subject: Re: V+=1 doesn't work if V is a reference to an integer array element
Date: Wed, 13 Jan 2021 22:40:17 +0100
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Le 13/01/2021 à 22:13, Chet Ramey écrivait :
The `-i' forces arithmetic evaluation, which makes this expand to the equivalent of `declare -n b=1'. That's an invalid name for a nameref,
which you'd see if you used `declare -n b=1' instead. The assignment
error causes `declare' to return an non-zero status, but the error
message checks the original value, not the result of arithmetic
evaluation, and the `a[0]' is valid.

I guess I never thought people would try to use integer variables as
namerefs, since nameref values can't be digits.

Sorry Chet, this does not make sense to me. There is no error message.
It look like to replied about Greg's post.

I still wonder how when performing b+=number, the referenced value of a[0] is multiplied by 2 and the added the number back to a[0]

If a[0] is 4 and you do b+=2, then a[0] would be updated to 10

The integer attribute to the nameref variable just make it work in arithmetic assignation var+=value without causing an error.

I don't understand why this exact sequence:

unset a; unset -n b; \
declare -ai a=('4'); \
declare -n b='a[0]'; \
typeset -ni b; \
declare -p a b; \
b+=2; \
declare -p a b

Produces this output of the a array:

declare -ai a=([0]="4")
declare -in b="a[0]"
declare -ai a=([0]="10")
declare -in b="a[0]"

I set the integer attribute to the nameref b after it has been assigned the "a[0]" string, so it keeps its string nameref value but is now also considered as an integer value.

Léa Gris

reply via email to

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