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() { 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() { 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. :) |
Quote:
Code:
arrayname=([subscript]=5678) |
Quote:
Code:
arrayname=([0]=5678) |
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. |