LinuxQuestions.org
Register a domain and help support LQ
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 03-06-2012, 02:19 PM   #1
gusthecat
LQ Newbie
 
Registered: Feb 2012
Posts: 19

Rep: Reputation: Disabled
reference bash array values, using a variable with a value of the array name


In my script I have several arrays and as part of the processing I figure out the names. Once
I have the name I assign it to a variable. Now the hard part. How do I process/reference the array based on the variable name. I have tried several things and best I can come up with is the first index only

ex.
array1=("hello gus" "goodbye world" "I need help")
refarray1=array1

How do I use refarray1 or manipulate refarray1 in order to get the values of the array.

thanks
 
Old 03-06-2012, 08:33 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Are you using a version of bash that supports associative arrays? They were introduced in version 4.0.
 
Old 03-07-2012, 01:38 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
Probably need to explain some more gus as I am a bit lost?
 
Old 03-07-2012, 08:45 AM   #4
gusthecat
LQ Newbie
 
Registered: Feb 2012
Posts: 19

Original Poster
Rep: Reputation: Disabled
thanks for the replies but I came up with a different solution.
 
Old 03-07-2012, 09:46 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
And would you like to share with the class so everyone may benefit?
 
Old 03-07-2012, 03:41 PM   #6
gusthecat
LQ Newbie
 
Registered: Feb 2012
Posts: 19

Original Poster
Rep: Reputation: Disabled
sorry I should have provided a bit more detail before marking solved.

Initially I didn't actually solve my original issue I just reworked the script to avoid the situation.

However, I didn't give up and did in fact find the solution. Now I'll rework the script again because its a better implementation.


array1=("hello gus" "goodbye world" "I need help")
refarray1=array1

I started with above and wanted to know how to use refarray1 to refer to the elements in the array1.
The solution was to correctly deference refarray1. I was not getting that part correct.

These 2 for loops produce same output

# using array1
for((x=0; x < ${#array1[@]}; x += 1))
do
echo ${array1[$x]}

done

#using refarray
eval no_elem=\${#${refarray1}[@]}
for((x=0; x < $no_elem; x += 1))
do
eval echo \${${refarray1}[$x]}

done

the 2 for loops are equivalent. the second version is where I was stuck

Last edited by gusthecat; 03-07-2012 at 03:46 PM. Reason: I hit submit by accident
 
  


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
[SOLVED] addition using array values into a variable not possible? olof_ Programming 4 11-21-2011 04:00 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
Bash indirect reference to array variable(s) sixerjman Programming 6 10-25-2006 11:18 AM


All times are GMT -5. The time now is 08:00 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration