LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-29-2009, 05:17 AM   #1
_kostek_
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Problem with getting string value from array


Hi All

I've got a problem...

a[0] = 22
echo $[a[0]]

echoes 22 - that works fine

a[0] = AB
echo $[a[0]]

echoes 0 - It doesn't work

a[0] = AB
echo ${a[0]}

echoes AB - that works fine as well

So my question is WHY "echo $[a[0]]" doesn't work properly with string and how I should declare
a[0] or do something else to make it work(maybe does it make difference running it in bash or ksh)??


It's necessary to make it work with $[a[0]] instead of ${a[0]}.

waiting for answers....

thanks
 
Old 10-29-2009, 06:05 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by _kostek_ View Post
It's necessary to make it work with $[a[0]] instead of ${a[0]}.
No. Not possible in bash.
The way to dereference an array member in bash is ${a[0]}.
 
Old 10-29-2009, 06:49 AM   #3
_kostek_
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hko View Post
No. Not possible in bash.
The way to dereference an array member in bash is ${a[0]}.
OK maybe I describe why is $[a[0]] so important

I put my scripts in some environment(I dont have permissions to change variable values in this environment). There is requirement in documentation regarding scripts to use $[a[0]] to make them flexible(administrator changes variable values and run the scripts).
When I use $[a[0]] in my script and run it under environment IT WORKS!!!
SO HOW IT IS DONE ??


In order to test my scripts I just want to simulate the environment, set the variables as I want avoiding changing scripts($[a[0]] to ${a[0]})

I hope it's clear :-)
 
Old 10-29-2009, 01:05 PM   #4
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
Quote:
Originally Posted by _kostek_ View Post
I put my scripts in some environment
This is very puzzling; I don't understand why bash is doing what you reported. I can reproduce what you reported and found it is doing some sort of arithmetic
Code:
c:~$ a[0]=22
c:~$ echo $[a[0]]
22
c:~$ a[0]=AB
c:~$ echo $[a[0]]
0
c:~$ a[0]=22A
c:~$ echo $[a[0]]
bash: 22A: value too great for base (error token is "22A")
I have not found anything illuminating in the GNU Bash Reference Manual, the POSIX Shell Command Language Specifications and the Korn Shell man page. To make matters worse, Google and heapr cannot search for "$["

What is the environment that you put your scripts in?
 
Old 10-29-2009, 02:08 PM   #5
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
The interpretation of the "$[]" is called Bash arithmetic expansion. The use of "[]" is apparently now deprecated and left at only "(())" . Here's a reference to it. You can try substituting the recommended "(())" for the "[]" and see that you get the same results.
 
Old 10-31-2009, 11:11 AM   #6
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
Quote:
Originally Posted by bgeddy View Post
The interpretation of the "$[]" is called Bash arithmetic expansion. The use of "[]" is apparently now deprecated and left at only "(())" . Here's a reference to it. You can try substituting the recommended "(())" for the "[]" and see that you get the same results.
Thanks bgeddy I wonder how old it is that it's not in the primary bash references but is on linux.about.com
 
  


Reply



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
To read a string into an array som_kurian Programming 7 12-06-2007 04:39 PM
Php String to Array joelhop Programming 2 12-18-2006 03:39 PM
String Array definiation problem nelnel Programming 1 09-16-2005 04:56 AM
String Input & Array Comparison Problem azucarmom Programming 2 03-13-2005 07:23 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

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

All times are GMT -5. The time now is 12:17 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