LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-09-2012, 07:38 PM   #1
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Rep: Reputation: 0
Decimal in array index


Hi,

Need some help in here...It seems that we cannot define a decimal in an array index...Whenever I do the below, it can't work..

subnet_array=([1.1]=US [5.2]=EU [2.3]=JPN)

Is there a workaround for this type of situation?

Thank a million..
 
Old 07-09-2012, 07:46 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

it looks like you want to use some sort of associative array or dictionary. However to provide help we will need to know what language you are using (it seems to not be one I'm familiar with, since I can't tell from the code you posted).

Evo2.
 
Old 07-09-2012, 08:01 PM   #3
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Original Poster
Rep: Reputation: 0
apologies...I'm just using Linux bash shell script....
 
Old 07-09-2012, 08:46 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Hi looks like you may have forgot to declare it as an associative array.
Try the following:
Code:
declare -A subnet_array
subnet_array=([1.1]=US [5.2]=EU [2.3]=JPN)
echo ${subnet_array["1.1"]}
HTH,

Evo2.

PS. I'd never used associative arrays in bash before, and found this
in a linux journal article that was returned as the top hit searching duckduckgo for "bash associative array":
http://duckduckgo.com/?q=bash%20associative%20array
 
Old 07-09-2012, 08:57 PM   #5
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks for the info....But I hit another problem...How do I reference it if the index is a variable?

I try using double quote, single quote but all not valid...Below is a simple code for iterate the subnet_array define previous and print out the index and the elements...It fails...Any idea?

###
echo "Array items and indexes:"
for index in ${!subnet_array[*]}
do
echo $index
printf "%4d: %s\n" $index ${subnet_array["$index"]}
done
###
 
Old 07-09-2012, 09:12 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Code:
declare -A subnet_array
subnet_array=([1.1]=US [5.2]=EU [2.3]=JPN)
index="1.1"
echo ${subnet_array[$index]}
Evo2.
 
1 members found this post helpful.
Old 07-10-2012, 01:48 AM   #7
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Original Poster
Rep: Reputation: 0
thanks heaps...This solve my problem...
 
Old 07-10-2012, 03:36 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark your question as SOLVED using the Thread Tools.
 
Old 07-10-2012, 07:20 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
This link should help you.

How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005


Note that standard arrays use only integers as indexes. Since the "[]" field operates in a mathematic context they can have arithmetic operations directly performed on them, and variables that contain integers don't have to (but can) be prefixed by "$".

Code:
array=( foo bar baz )

for i in 0 1 2 ; do
	echo "Array index $i is ${array[i]}, and the next one is ${array[i+1]:-empty}"
done

#output:
Array index 0 is foo, and the next one is bar
Array index 1 is bar, and the next one is baz
Array index 2 is baz, and the next one is empty

Associative arrays, as used here, treat the indexes as text strings, and so any combinations of characters can be used. Since the "[]" field does not operate in an arithmetic environment all variables must have "$" in front of them in order for them to expand.

Associative arrays have to be explicitly declared before you can use them as such. You also can't guarantee what order the indexes will come out in when you use "*" or "@", as they are stored according to the shell's internal memory mapping.

Code:
declare -A array
array=( [one]=foo [dos]=bar [san]=baz )

for i in "${!array[@]}" ; do
	echo "Array index $i is ${array[$i]}"
	echo "I won't know what the next one is, if any, until the next iteration of the loop"
done

#output:
Array index dos is bar
I won't know what the next one is, if any, until the next iteration of the loop
Array index one is foo
I won't know what the next one is, if any, until the next iteration of the loop
Array index san is baz
I won't know what the next one is, if any, until the next iteration of the loop
 
Old 07-10-2012, 09:28 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
A thought that occurs to me also is ... "it's not such a good idea to use Bash as a programming language, even if you 'can.' "

In Unix/Linux, you can write "a command" in any language you please, and no one will be the wiser. But these various languages have one over-arching advantage that Bash does not: they were designed to be "programming languages." In my opinion, the only Unix shell that ever had a true "built-in programming language," as its designer's expressed intention, was and is the Korn shell.

I'm pragmatic and lazy. When I want to write "a computer program," I want to use a tool built for that job, and I want it to let me do as much as possible while writing as little "new, original" code as possible.
 
  


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
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
binary to decimal conversion without using array (C programming) linuxi Programming 12 11-10-2006 05:21 PM
Perl: printing array's index and value craig467 Programming 8 08-30-2006 03:50 PM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM

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

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