[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: Oğuz
Subject: Re: V+=1 doesn't work if V is a reference to an integer array element
Date: Thu, 14 Jan 2021 07:55:45 +0200

14 Ocak 2021 Perşembe tarihinde Léa Gris <lea.gris@noiraude.net> yazdı:

> 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]"

Seems to be another bug. `bind_variable_internal' calls both
`assign_array_element' and `make_variable_value' with ASS_APPEND in flags (
https://git.savannah.gnu.org/cgit/bash.git/tree/variables.c#n3140 ), the
latter returns 6 (4 + 2), the former adds it to a[0]'s value.

> 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]