Hi,
I'm trying to decrement the values contained in an array. I'm reading a file which has a list of indices and for each one I want to decrement the value held in the array at that index. Seems simple but I'm not sure what I'm doing wrong.
Code:
#!/bin/bash
instanceFile=$1
solutionFile="${instanceFile/.txt/.sol}"
#echo "Inst: $instanceFile, Sol: $solutionFile"
capacity=$(head -n 1 $instanceFile)
binItems=()
while read -r size amount; do
binItems[$size]=$amount
done < <(tail -n +4 $instanceFile)
# Just verifying that the array is instantiated properly
for i in ${!binItems[*]}; do
echo "[$i] ${binItems[$i]}"
done
while read -d " " size; do
echo -n "Pre: ${binItems[$size]} "
binItems[$size]=$((${binItems[$size]}-1))
echo "Post: ${binItems[$size]} "
done < $solutionFile
The instance file is:
100
26
23
3 1
6 2
8 2
The solution file contains:
3 8
6 8
6
The error I'm getting from bash is:
-1")syntax error: invalid arithmetic operator (error token is "
I can't see any errors with the way I'm doing the arithmetic or the array assignment? Can anyone shed some light on this?
Thanks,
Alastair