I am having trouble with some code and I've discovered that I am getting multiple identical keys in my associative array, which I would have not expected possible. I have tried to get this behaviour in a simple test case and am coming up short - if I assign a second key/value pair to the associative array, the old one is gone (which I would expect).
Here is the code that prints out the key/value pairs:
local key=''
local value=''
for key value in ${(kv)grid}; do
echo "$key -> $value" >&2
done
and here is some of the output (after I sorted it)
"1000,1000" -> #
"1000,1000" -> #
"1000,1000" -> #
"1000,1000" -> #
"1000,1001" -> #
"1000,1001" -> #
"1000,1001" -> #
"1000,1001" -> #
"1000,1002" -> #
"1000,1002" -> #
"1000,1002" -> #
"1000,1002" -> #
"1000,1003" -> #
You can see there are multiple key/value pairs with the same key (and in some cases the same value too).
This occurs on both z/OS Zsh 5.8.1 and Mac Zsh 5.9
I expect I have a bug in my code, but if someone knows how I can be getting these multiple key/value pairs into the associative array, I would be grateful.
Edit: adding more information as requested:
Here is how I declare the grid:
declare -A grid
Here is how I set a 'cell' in the grid:
set_cell()
{
local srow=$1
local scol=$2
local sc=$3
local skey="${srow},${scol}"
grid["${skey}"]="$sc"}
}
Here is how I 'get' the value of a cell:
get_cell()
{
local grow=$1
local gcol=$2
local gkey="${grow},${gcol}"
local gval=${grid[(k)"${gkey}"]}
echo "${gval}"
return 0
}
I call the get and set routines as:
local c
c=$(get_cell ${row} ${lcol})
and:
local out='O'
set_cell ${row} ${rcol} "${out}"
where the row and col variables are integral.
I also added:
echo "Type of grid: ${(t)grid}" >&2
just before the loop dumping out the values and I get:
Type of grid: association