LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2009, 07:51 AM   #1
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 07-05-2009, 01:54 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 07-09-2009, 02:42 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248

Original Poster
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by ntubski View Post
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.
 
Old 07-14-2009, 06:08 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248

Original Poster
Blog Entries: 8

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
Using global variable in awk (bash script) kopeda Programming 2 04-24-2007 01:47 AM
How do I create a global variable YankeeFan Linux - Newbie 1 11-13-2006 05:33 PM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration