LinuxQuestions.org
Review your favorite Linux distribution.
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 11-28-2010, 06:16 AM   #1
jsteel
Member
 
Registered: Mar 2007
Location: England
Distribution: Arch
Posts: 392

Rep: Reputation: 34
Bash array - more than one value per item?


Hi,

Hopefully something simple but can't seem to find an answer. Is it possible to have an array in Bash that can hold more than one value per item?

For example I would like an array like this:

Entry 1: apple, green
Entry 2: banana, yellow

And be able to call the fruit names and their colour in a list. Something like:

for fruit in "${array[@]}"
do
echo a $fruit is $colour
done

If that is possible, is there a limit to values per item? For example some entries in an array could be:

Entry 1: apple, green, round, pips, tree
Entry 2: banana, yellow, long, skin, tree

And I would like to pick out the values such as #3 being "round" and "long".

Thanks
 
Old 11-28-2010, 06:31 AM   #2
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
The classic solution when a language supports only 1 dimensional arrays is an array for each attribute, following your example a name array, a colour array ... then you can loop through:
Code:
for ((i=0; i<${names[*]}; i++))
do
   echo a ${names[i]} is ${colours[i]}
done
This can be extended using content addressable arrays so that, for example, the value of colours[apple] is green.

Alternatively you can have a single array with lists of attributes such as the "apple, green, round, pips, tree" in your example. To access an individual attribute you need to parse the list; important that the list separator character cannot appear in the data.
 
Old 11-28-2010, 06:55 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Another possibility to could depend on how well you know the data. If you know ahead of time how many elements to each array entry and what they refer to you could do something like:
Code:
array=( "apple,green,round,pips,tree" "banana,yellow,long,skin,tree" )

for entry in ${array[*]}
do
    set - $(echo "$entry" | tr ',' ' ')

    echo "name = $1"
    echo "colour = $2"
    echo "to grow = $3"
    echo "grows on = $4"
done
 
Old 11-30-2010, 02:28 PM   #4
jsteel
Member
 
Registered: Mar 2007
Location: England
Distribution: Arch
Posts: 392

Original Poster
Rep: Reputation: 34
Thanks for the replies; both great!
 
  


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
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
bash scripting help want to execute external function on current item. Benanzo Linux - Software 3 02-03-2007 02:45 PM
removing item from array niteshadw Programming 1 08-01-2005 06:06 PM

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

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