4

I am writing a program and trying to break up data, which is stored in an array, in order to make it run faster. I am attempting to go about it this way:

    data_to_analyze=(1 2 3 4 5 6 7 8 9 10)
    #original array size
    dataSize=(${#data_to_analyze[@]})

    #half of that size
    let samSmall="$dataSize/2"

    #the other half
    let samSmall2=("$dataSize - $samSmall -1")

    #the first half
    smallArray=("${data_to_analyze[@]:0:$samSmall}")

    #the rest
    smallArray2=("${data_to_analyze[@]:$samSmall:$samSmall2}")

    #an array of names(which correspond to arrays)
    combArray=(smallArray smallArray2)
    sizeComb=(${#combArray[@]})

    #for the length of the new array
    for ((i=0; i<= $sizeComb ; i++)); do
        #through first set of data and then loop back around for the second arrays data? 
        for sample_name in ${combArray[i]}; do
            command
            wait
            command
            wait
        done

What I imagine this does is gives only the first array of data to the for loop at first. When the first array is done it should go through again with the second array set.

That leaves me with two questions. Is combArray really passing the two smaller arrays? And is there a better way?

2
  • 1
    combArray=(smallArray smallArray2), I think. Also, no need for parentheses around the ${#array[@]} construct. Commented Jul 9, 2012 at 20:57
  • There are multiple syntax errors in the above that make it hard to figure out what you're really after. For example, there's an extra parenthesis on line 9 and a dollar sign on line 18 I don't think you meant. Commented Jul 9, 2012 at 21:03

2 Answers 2

5

You can make a string that looks like an array reference then use it to indirectly access the elements of the referenced array. It even works for elements that contain spaces!

combArray=(smallArray smallArray2)
for array in "${combArray[@]}"
do
    indirect=$array[@]    # make a string that sort of looks like an array reference
    for element in "${!indirect}"
    do
        echo "Element: $element"
    done
done
Sign up to request clarification or add additional context in comments.

Comments

0
#!/bin/bash
data_to_analyze=(1 2 3 4 5 6 7 8 9 10)
dataSize=${#data_to_analyze[@]}
((samSmall=dataSize/2,samSmall2=dataSize-samSmall))
smallArray=("${data_to_analyze[@]:0:$samSmall}")
smallArray2=("${data_to_analyze[@]:$samSmall:$samSmall2}")
combArray=(smallArray smallArray2)
sizeComb=${#combArray[@]}
for ((i=0;i<$sizeComb;i++));do
    eval 'a=("${'${combArray[i]}'[@]}")'
    for sample_name in "${a[@]}";do
        ...
    done 
done

EDIT: removed the double quotes near ${combArray[i]} and replaced <= by < in for

3 Comments

eval 'a=("${'"${combArray[i]}"'[@]}")' This line gets a bad substitution error
the problem was maybe due to the <= replaced by < in for condition which was copied from the source
I actually ending up using this method. It is pretty fantastic. Thanks!

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.