0

I have this array:

declare -A modeAndAction
modeAndAction[category]=create
modeAndAction[attribute]=create
modeAndAction[customers]=create
modeAndAction[newsletter]=create
modeAndAction[vouchers]=create
modeAndAction[products]=create
modeAndAction[orders]=create

Which outputs this:

Starting import of mode : attribute
With action: create
Starting import of mode : category
With action: create
Starting import of mode : newsletter
With action: create
Starting import of mode : vouchers
With action: create
Starting import of mode : orders
With action: create
Starting import of mode : customers
With action: create
Starting import of mode : products
With action: create

But I expect this output:

Starting import of mode : category
With action: create
Starting import of mode : attribute
With action: create
Starting import of mode : customer
With action: create
Starting import of mode : newsletter
With action: create
Starting import of mode : vouchers
With action: create
Starting import of mode : products
With action: create
Starting import of mode : orders
With action: create

So everything moved in the output, ignoring its declartion, but I dont understand why. I want the modes (+ its action) to be called exactly in the order I have the array declared in.

Bash Version:

GNU bash, version 4.2.51(1)-release (x86_64-pc-linux-gnu)

//Edit I found out that if I have this array

modeAndAction[AAAAAAAAA]=create;
modeAndAction[B]=create;
modeAndAction[C]=create;
modeAndAction[D]=create;

I get this output:

Starting import of mode : B
With action: create
Starting import of mode : C
With action: create
Starting import of mode : D
With action: create
Starting import of mode : AAAAAAAAA
With action: create

So it auto-sorts it by key lenght.

//Edit2 Code for responsible for the output

for i in "${!modeAndAction[@]}"
    do
        echo "Starting import of mode : $i"
        echo "With action: ${modeAndAction[$i]}"
    done
paskl
  • 589
  • 4
  • 25

1 Answers1

1

Solved it by using non assoc array keys now:

    modeAndAction[0]=category
    modeAndAction[1]=attribute
    modeAndAction[2]=customers
    modeAndAction[3]=newsletter
    modeAndAction[4]=vouchers
    modeAndAction[5]=products
    modeAndAction[6]=orders


    for i in "${!modeAndAction[@]}"
    do
        echo "Starting import of mode : ${modeAndAction[$i]} with action create"
        # ...
    done
paskl
  • 589
  • 4
  • 25
  • 1
    An equivalent way to declare that array: `modeAndAction=(category attribute customers newsletter vouchers products orders)` – glenn jackman Jan 20 '15 at 17:09