LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-06-2012, 05:00 PM   #1
emanation
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
Store a value to linked name of variable or array


Is it possible in Bash to store a value to variable with linked name?
I mean when variable name is stored at other variable, like here

Code:
A="abcd dcba"
B=A
C=${!B}
echo $C
So, I need to store to ${!B}

In fact I need more than this. I have arrays
Code:
small=( 'one' 'two' 'three' 'four' )
big=( 'ONE' 'TWO' 'THREE' 'FOUR' )
digit=( 1 2 3 4 )
totalNames=( 'small' 'big' 'digit' )
and I get needed values like
Code:
for name in ${totalNames[@]}; do
temp1=$( eval echo \${$name[0]} )
temp2=$( eval echo \${$name[1]} )
done
or without cycle
Code:
temp1=$( eval echo \${${totalNames[0]}[0]} )
temp2=$( eval echo \${${totalNames[0]}[1]} )
after some actions with temp1 and temp2
i need to store values to ${$name[2]} and ${$name[3]} in cycle
or to ${${totalNames[0]}[2]} and ${${totalNames[0]}[3]} out of cycle.
Is it possible?
Thanks a lot.

Last edited by emanation; 02-08-2012 at 10:38 AM.
 
Old 02-06-2012, 05:08 PM   #2
emanation
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
if it's not possible, maybe it's possible to create new variable in cycle with name result$name which in fact must be
Code:
$resultsmall
$resultbig
$resultdigit
it also solves my needs.
Thanks.
 
Old 02-08-2012, 10:41 AM   #3
emanation
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
inside cycle
Quote:
eval $name[0]='New\ value'
out cycle
Quote:
eval ${totalNames[0]}[0]='New\ value'
new variable name
Quote:
eval result${totalNames[0]}='New\ value'

Last edited by emanation; 02-08-2012 at 10:43 AM.
 
  


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] How to store output of command to an array kasparov29 Linux - General 4 11-14-2011 02:26 PM
store a 2,4 tree in a single array johndtii Programming 1 10-30-2010 02:27 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
To store N numbers in an array som_kurian Programming 10 12-16-2007 03:31 AM
store output of cmd in array? h/w Programming 6 10-09-2003 08:46 PM


All times are GMT -5. The time now is 12:21 AM.

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