Array of arrays in bash

前端 未结 6 1127
日久生厌
日久生厌 2020-12-24 05:09

I\'m attempting to read an input file line by line which contains fields delimited by periods. I want to put them into an array of arrays so I can loop through them later on

6条回答
  •  执念已碎
    2020-12-24 06:03

    I use Associative Arrays and use :: in the key to denote depth. The :: can also be used to embed attributes, but that is another subject,...

    declare -A __myArrayOfArray=([Array1::Var1]="Assignment" [Array2::Var1]="Assignment")
    

    An Array under Array1

    __myArrayOfArray[Array1::SubArray1::Var1]="Assignment"
    

    The entries in any array can be retrieved (in order ...) by ...

    local __sortedKeys=`echo ${!__myArrayOfArray[@]} | xargs -n1 | sort -u | xargs`
    for __key in ${__sortedKeys}; do
        #
        # show all properties in the Subordinate Profile "Array1::SubArray1::"
        if [[ ${__key} =~ ^Array1::SubArray1:: ]]; then
            __property=${__key##Array1::SubArray1::}
            if [[ ${__property} =~ :: ]]; then
                echo "Property ${__property%%:*} is a Subordinate array"
            else
                echo "Property ${__property} is set to: ${__myArrayOfArray[${__key}]}"
            fi
        fi 
    done
    

    THE list of subordinate "Profiles" can be derived by:

    declare -A __subordinateProfiles=()
    local __profile
    local __key
    for __key in "${!__myArrayOfArray[@]}"; do
        if [[ $__key =~ :: ]]; then
            local __property=${__key##*:}
            __profile=${__key%%:*}
            __subordinateProfiles[${__profile}]=1
        fi   
    done
    

提交回复
热议问题