1

I made an associative array as follows. To give a few details, the keys refer to specific files because I will be using this array in the context of a larger script (where the directory containing the files will be a getopts argument).

declare -A BAMREADS
echo "BAMREADS array is initialized"

BAMREADS[../data/file1.bam]=33285268
BAMREADS[../data/file2.bam]=28777698
BAMREADS[../data/file3.bam]=22388955

echo ${BAMREADS[@]}  # Output: 22388955 33285268 28777698
echo ${!BAMREADS[@]} # Output: ../data/file1.bam ../data/file2.bam ../data/file3.bam

So far, this array seems to behave as I expect. Now, I want to build another associative array based on this array. To be specific: my second array will have the same keys as my first one but I want to divide the values by a variable called $MIN.

I am not sure which of the following strategies is best and I can't seem to make either work.

Strategy 1: copy the array and modify the array?

MIN=33285268

declare -A BRAMFRACS
echo "BAMFRACS array is initialized"
BAMFRACS=("${BAMREADS[@]}")

echo ${BAMFRACS[@]}  # Output: 22388955 33285268 28777698
echo ${!BAMFRACS[@]} # Output: 0 1 2

This is not what I want for the keys. Even if it works, I would then need to perform the operation I mentioned on all the values.

Stragegy 2: build the second array when looping through the first.

MIN=33285268

declare -A BRAMFRACS
echo "BAMFRACS array is initialized"

for i in $(ls $BAMFILES/*bam)
do
    echo $i
    echo ${BAMREADS[$i]}
    BAMFRACS[$i] = ${BAMREADS[$i]} 
done

echo ${BAMFRACS[@]}
echo ${!BAMFRACS[@]}


# When I run this, I get the following error which I am unsure how to solve:

../data/file1.bam
33285268
script.bash: line 108: BAMFRACS[../data/file1.bam]: No such file or directory
../data/file2.bam
28777698
script.bash: line 108: BAMFRACS[../data/file2.bam]: No such file or directory
../data/file3.bam
22388955

UPDATE Updated the strategy2 and it's output to be clearer. Thanks

0

1 Answer 1

3

In both

declare -A BRAMFRACS

should be

declare -A BAMFRACS

that is, you have an additional 'R' in the name, the reason for one of the errors is that BAMFRACS was an indexed array (the default), not associative.

and in strategy 2:

BAMFRACS[$i] = ${BAMREADS[$i]} 

should be

BAMFRACS[$i]=${BAMREADS[$i]

that is, no spaces around the = are allowed. BAMFRACS[$i] is seen as a command, since commands are delimited by whitespace.

By the way,

for i in $(ls $BAMFILES/*bam) 

could be written as:

for i in "$BAMFILES"/*bam

Try:

MIN=33285268

declare -A BAMFRACS

for key in "${!BAMREADS[@]}"
do
    val=${BAMREADS[$key]}
    BAMFRACS[$key]=$((val/MIN))
done

echo ${BAMFRACS[@]}  
echo ${!BAMFRACS[@]} 

Gives:

1 0 0
../data/file1.bam ../data/file2.bam ../data/file3.bam
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.