Bash: iterate over bash array in the order the elements were declared.
Jump to:
Project: | Programming |
Component: | Code |
Category: | support request |
Priority: | normal |
Assigned: | Unassigned |
Status: | active |
Related pages: | #5431: bash: arrays :-:-: #5481: bash: for ... do iteration |
Tags: | bash script |
Description
When I iterate over a map (associative array), the order in which the elements in the array are iterated depend on the type of keys.
If my keys are 1, then 'b' (defined in that order), I get them back in the same order: 1, then 'b'.
If my keys are 1, then 2 (defined in that order), I get them in reverse order: 2, then 1.
This is without changing the for loop, nor changing the order in which the elements are declared, simply changing the keys.
Comments
#1
#2
Oh!
It's the way I iterate over the keys.
They are listed in alphabetical order.
I've spent a few hours trying to understand how to implement bash array indirection.
It's not critical for now, but I wonder which way to have elements iterated over in the same order they were declared.