Loop over tuples in bash?

后端 未结 12 1535
说谎
说谎 2020-12-04 21:00

Is it possible to loop over tuples in bash?

As an example, it would be great if the following worked:

for (i,j) in ((c,3), (e,5)); do echo \"$i and $         


        
12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 21:21

    But what if the tuple is greater than the k/v that an associative array can hold? What if it's 3 or 4 elements? One could expand on this concept:

    ###---------------------------------------------------
    ### VARIABLES
    ###---------------------------------------------------
    myVars=(
        'ya1,ya2,ya3,ya4'
        'ye1,ye2,ye3,ye4'
        'yo1,yo2,yo3,yo4'
        )
    
    
    ###---------------------------------------------------
    ### MAIN PROGRAM
    ###---------------------------------------------------
    ### Echo all elements in the array
    ###---
    printf '\n\n%s\n' "Print all elements in the array..."
    for dataRow in "${myVars[@]}"; do
        while IFS=',' read -r var1 var2 var3 var4; do
            printf '%s\n' "$var1 - $var2 - $var3 - $var4"
        done <<< "$dataRow"
    done
    

    Then the output would look something like:

    $ ./assoc-array-tinkering.sh 
    
    Print all elements in the array...
    ya1 - ya2 - ya3 - ya4
    ye1 - ye2 - ye3 - ye4
    yo1 - yo2 - yo3 - yo4
    

    And the number of elements are now without limit. Not looking for votes; just thinking out loud. REF1, REF2

提交回复
热议问题