LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   create a global associative array variable inside a function of a bash script (https://www.linuxquestions.org/questions/programming-9/create-a-global-associative-array-variable-inside-a-function-of-a-bash-script-737463/)

konsolebox 07-03-2009 07:51 AM

create a global associative array variable inside a function of a bash script
 
Hello. I'm not sure there's a solution to this but just in case guys.. do you know any way how to create a global associative array inside a function?

In the following code that I'll present, the associative array can never be made global since by default anything that is declared by the 'declare' built-in command (i think also typeset) if the command is inside a function will be only made local inside the function.

This won't really be much of a problem for indexed arrays since they can be easily initialized anywhere by 'arrayname=()' syntax format but not for associative arrays since they can only be created using 'declare -A' (I hope I'm wrong and please correct me if I am :)).

I already searched the web with keywords like 'bash global associative' but none seems to give good results. I also already asked gnu about this but there's no reply. Maybe they're busy or I don't know.

Anyway with the following code:
Code:

makeGlobal() {
        declare -A globalArray
        globalIndexedArray=("1234")
        globalArray[subscript]=5678
}

makeGlobal
echo "${globalArray[subscript]}"
echo "${globalIndexedArray[@]}"

We should expect that the only output should be 1234 and no 5678.

Just in case you'll wonder why I need to create associative arrays inside a function, that's because I have a set of scripts that calls each other within a function like:
Code:

include() {
    local file=$1 args=("${@:2}")
    ...
    source "$absfilepath" "${args[@]}"
    ...
}

include "somewhere/log.sh"

as you can see, include() is a function so if somewhere/log.sh has a global associative array, after the function returns, the variable will be lost.

Associative array variables are not yet an important functionality for me yet but I've separated my includer scripts which I call pkgr. Before pkgr was just part of a single project but I found its usefulness and turned it into a single project instead. Pkgr currently does well for its concept in most kinds of shells (sh, bash, ksh, and zsh) even with compilation of the scripts to make them one but it might lack its usefulness for future scripts for bash that will be using global associative variables. Zsh by the way have -g option in the declare builtin that flags global in variables even if inside a function so no problem but how about bash?

So any idea? By the way, of course I'm referring to the new version of bash and that's 4.0.

Thanks in advance. :)

ntubski 07-05-2009 01:54 PM

Quote:

Originally Posted by konsolebox (Post 3595474)
This won't really be much of a problem for indexed arrays since they can be easily initialized anywhere by 'arrayname=()' syntax format but not for associative arrays since they can only be created using 'declare -A' (I hope I'm wrong and please correct me if I am :)).

Based on my reading of the Bash Manual I think that you could do
Code:

arrayname=([subscript]=5678)
untested since I don't have bash version 4.

konsolebox 07-09-2009 02:42 AM

Quote:

Originally Posted by ntubski (Post 3597572)
Code:

arrayname=([subscript]=5678)

I tried that already before but I don't remember why I didn't consider it as a solution. Perhaps if arrayname is not previously declared as an associative array, the statement will be just like an indexed array declaration:
Code:

arrayname=([0]=5678)
i'll try it again later though. thanks.

konsolebox 07-14-2009 06:08 AM

Forgot to post back. The statement didn't work by the way. As expected it's still treated as an indexed array.


All times are GMT -5. The time now is 04:35 AM.