bug-bash
[Top][All Lists]
Advanced

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

Strange behaviour using negative index in arrays


From: felix
Subject: Strange behaviour using negative index in arrays
Date: Sat, 02 Jan 2021 13:40:43 +0100

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -Wno-parentheses -Wno-format-security
uname output: Linux medium 4.19.0-9-amd64 #1 SMP Debian 4.19.118-2 (2020-04-29) 
x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: 4
Release Status: release

Description:
        Assigning array with negative number not alway detected as an error

Trying to sort integers, I was using:

    f() { local a i;for i;{ a[$i]=;echo $i;};a=(${!a[@]});echo $a;}

This could work fine with any positive numbers, but trying:

    f 34 56 12 56 53 -12 -21
this return quietly `12` ?? (No error)

( Under 4.2.37(1)-release, I get: ``-bash: a[$i]: bad array subscript'' )

But with ``my'' 5.0.3(1)-release, as with new 5.1.4(1)-release, no error...

Further:

    f() { local a i c;for i;{ a[$i]=$((c++));echo $i;};declare -p a ;
           a=(${!a[@]}); declare -p a; echo $a ;}

    f 34 56 12 56 53 -12 -21
    34
    56
    12
    56
    53
    -12
    -21
    declare -a a=([12]="2" [34]="0" [36]="6" [45]="5" [53]="4" [56]="3")
    declare -a a=([0]="12" [1]="34" [2]="36" [3]="45" [4]="53" [5]="56")
    12

Where 5th value is 45 instead of -12 and 6th is 36 instead of -21
 45 + 12 = 57,  36 + 21 = 57 ... (highest submited value was 56!)??

    f 34 56 12 1000 53 -12 -21
    34
    56
    12
    1000
    53
    -12
    -21
    declare -a a=([12]="2" [34]="0" [53]="4" [56]="1" [980]="6" [989]="5" 
[1000]="3")
    declare -a a=([0]="12" [1]="34" [2]="53" [3]="56" [4]="980" [5]="989" 
[6]="1000")
    12

Then now:
    989 + 12 = 1001, 980 + 21 = 1001 ( and highest value was 1000 :)


... And happy new year!!



reply via email to

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