LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-05-2010, 10:24 PM   #1
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
Question problem in retrieving value from a variable


Hello guys!!

In one of my scripts, I take input in the following manner
Code:
for i in $(seq $r)
   do
      for j in $(seq $c) 
	  do
	     read -p"a[$i][$j]=" a$i$j
	  done
   done
this code declares separate variables each time it enters the inner for loop.
Now the question is how shall I print each variable separately using a generalized form. I tried writing the following within a for loop
Code:
echo $(echo a$(echo $i)$(echo $j))
which did yield no result. So what shall I write?????

Please help!!!!
 
Old 02-05-2010, 10:55 PM   #2
bmxcess
Member
 
Registered: Jan 2009
Location: Brisbane, Australia
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65

Rep: Reputation: 19
As far as I know bash only uspports one diimensional arrays. So try something like this in your loop.

Code:
Rows =  10 # number of rows in your array
let "index = $i * $Rows + $j"
a[$index]="some value"
# a[$i][$j]

then you can :

Code:
echo ${a[$index]}

Last edited by bmxcess; 02-05-2010 at 10:57 PM.
 
Old 02-05-2010, 10:58 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
In the statement read -p"a[$i][$j]=" a$i$j, a$i$j is evaluated and is used as the name of the variable used by the read command to store input. Here's an example command prompt session to illustrate that
Code:
c:~$ i=2
c:~$ j=5
c:~$ read a$i$j
my_input
c:~$ echo $i
2
c:~$ echo $j
5
c:~$ echo $a25
my_input
You can access such dynamically named variables in a script by using the eval command
Code:
eval echo \$a$i$j
The eval command instructs the shell to evaluate eval's arguments and then execute the resulting expression. Evaluating echo \$a$i$j results in echo $a25 (assuming $i has value 2 and $j has value 5). Note how the first $ is "escaped" with a \ to remove its special meaning during the first evaluation.

Last edited by catkin; 02-05-2010 at 10:59 PM. Reason: English as she is wrotten
 
Old 02-05-2010, 11:18 PM   #4
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285

Original Poster
Blog Entries: 1

Rep: Reputation: 37
Thanks to both of you....

I shall remember what bmxcess has said....it was a good trick

As for Catkin, I know this could have been solved by 'eval'...but I don't want to use it...so can you just provide me with a substitute of it...I mean something like "$()".
 
  


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
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Problem retrieving files from a hard drive pueblonative Linux - Hardware 2 04-30-2007 06:55 AM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM
Problem retrieving mail using PoP jones139 Linux - Networking 4 06-09-2005 03:10 AM
imap problem on retrieving email htm Linux - Software 0 04-22-2004 09:29 AM

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

All times are GMT -5. The time now is 04:44 PM.

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