|
Home > Archive > Unix Shell > October 2006 > Alternative to usual BASH Arrays.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Alternative to usual BASH Arrays.
|
|
|
| Hi,
I am almost certain I figured out and implemented a solution to the problem
below, but I've forgotten what it is, can't find the file(s) on any of my
computers and my brain is like spaghetti at the moment.
--------- BEGIN BASH SCRIPT ---------
#!/bin/bash
# Arrays of the kind assigned with 'name[number]'
# and accessed with '${name[number]}' need not be
# used; there is another way: code to create
# variables as 'namenumber', where 'name' stays
# the same, but 'number' is incremented after the
# creation of the previous one: e.g.:
# 'fruit1=apple', 'fruit2=orange' and
# 'fruit3=pear'.
number=0
for each in apple orange pear; do
number=$((number+1))
# On the first iteration of this
# for-do-done loop, create a variable
# called 'fruit1' and assign it the
# current value of 'each'. On the
# second iteration of this for-do-done
# loop, create a variable called 'fruit2'
# and assign it the current value of
# 'each'. And so on.
fruit$number=$each
done
# The above code does _not_ work. Is there a
# way of creating a variable name that has a
# variable name in it? I tried the following:
# 'fruitnumber=fruit$number', but, then, how do
# you assign a value? Doing:
# 'fruitnumber=$each' is no good.
echo \$number is $number
count=0
while ((number>count)); do
count=$((count+1))
# On the first iteration of this
# while-do-done loop, output the value
# of 'fruitnumber', which value will be
# 'apple'. On the second iteration of
# this while-do-done loop, output the
# value of 'fruitnumber', which value
# will be 'orange'. And so on.
echo $fruit$number
done
# The above code does _not_work. The
# interpreter needs to understand that I want
# the value of the variable 'fruitnumber'.
echo \$count is $count
--------- END BASH SCRIPT ---------
Thanks in advance for any help.
Yours,
Gary Hayward.
| |
|
| Gazza wrote:
> Hi,
>
> I am almost certain I figured out and implemented a solution to the
> problem below, but I've forgotten what it is, can't find the file(s) on
> any of my computers and my brain is like spaghetti at the moment.
>
> --------- BEGIN BASH SCRIPT ---------
>
> #!/bin/bash
>
> # Arrays of the kind assigned with 'name[number]'
> # and accessed with '${name[number]}' need not be
> # used; there is another way: code to create
> # variables as 'namenumber', where 'name' stays
> # the same, but 'number' is incremented after the
> # creation of the previous one: e.g.:
> # 'fruit1=apple', 'fruit2=orange' and
> # 'fruit3=pear'.
>
> number=0
> for each in apple orange pear; do
> number=$((number+1))
> # On the first iteration of this
> # for-do-done loop, create a variable
> # called 'fruit1' and assign it the
> # current value of 'each'. On the
> # second iteration of this for-do-done
> # loop, create a variable called 'fruit2'
> # and assign it the current value of
> # 'each'. And so on.
> fruit$number=$each
> done
>
> # The above code does _not_ work. Is there a
> # way of creating a variable name that has a
> # variable name in it? I tried the following:
> # 'fruitnumber=fruit$number', but, then, how do
> # you assign a value? Doing:
> # 'fruitnumber=$each' is no good.
>
> echo \$number is $number
>
> count=0
> while ((number>count)); do
> count=$((count+1))
> # On the first iteration of this
> # while-do-done loop, output the value
> # of 'fruitnumber', which value will be
> # 'apple'. On the second iteration of
> # this while-do-done loop, output the
> # value of 'fruitnumber', which value
> # will be 'orange'. And so on.
> echo $fruit$number
> done
>
> # The above code does _not_work. The
> # interpreter needs to understand that I want
> # the value of the variable 'fruitnumber'.
>
> echo \$count is $count
>
> --------- END BASH SCRIPT ---------
>
> Thanks in advance for any help.
>
> Yours,
> Gary Hayward.
Hi,
I have come up with this, but it uses 'read'. It works, but I'm almost sure
I found a way not using 'read'.
--------- BEGIN BASH SCRIPT ---------
#!/bin/bash
# Assign fruits:
number=0
for fruit in apple orange pear; do
number=$((number+1))
read fruit$number<<<$fruit
fruitnumber=fruit$number
echo ${!fruitnumber} # If need to show fruits here.
done
# Show fruits:
count=0
while ((number>count)); do
count=$((count+1))
fruitcount=fruit$count
echo ${!fruitcount}
done
--------- END BASH SCRIPT ---------
Yours,
Gary Hayward.
| |
|
| Hi,
In my 'read' script:
> echo ${!fruitnumber} # If need to show fruits here.
use:
echo fruit$number = ${!fruitnumber} # If need to show fruits here.
> echo ${!fruitcount}
use:
echo fruit$count = ${!fruitcount}
Yours,
Gary Hayward.
| |
|
|
Gazza wrote:
> I am almost certain I figured out and implemented a solution to the problem
> below, but I've forgotten what it is, can't find the file(s) on any of my
> computers and my brain is like spaghetti at the moment.
>
> --------- BEGIN BASH SCRIPT ---------
>
> #!/bin/bash
>
> # Arrays of the kind assigned with 'name[number]'
> # and accessed with '${name[number]}' need not be
> # used; there is another way: code to create
> # variables as 'namenumber', where 'name' stays
> # the same, but 'number' is incremented after the
> # creation of the previous one: e.g.:
> # 'fruit1=apple', 'fruit2=orange' and
> # 'fruit3=pear'.
>
> number=0
> for each in apple orange pear; do
> number=$((number+1))
> # On the first iteration of this
> # for-do-done loop, create a variable
> # called 'fruit1' and assign it the
> # current value of 'each'. On the
> # second iteration of this for-do-done
> # loop, create a variable called 'fruit2'
> # and assign it the current value of
> # 'each'. And so on.
> fruit$number=$each
> done
>
> # The above code does _not_ work. Is there a
> # way of creating a variable name that has a
> # variable name in it? I tried the following:
> # 'fruitnumber=fruit$number', but, then, how do
> # you assign a value? Doing:
> # 'fruitnumber=$each' is no good.
>
> echo \$number is $number
>
> count=0
> while ((number>count)); do
> count=$((count+1))
> # On the first iteration of this
> # while-do-done loop, output the value
> # of 'fruitnumber', which value will be
> # 'apple'. On the second iteration of
> # this while-do-done loop, output the
> # value of 'fruitnumber', which value
> # will be 'orange'. And so on.
> echo $fruit$number
> done
>
> # The above code does _not_work. The
> # interpreter needs to understand that I want
> # the value of the variable 'fruitnumber'.
>
> echo \$count is $count
>
> --------- END BASH SCRIPT ---------
>
> Thanks in advance for any help.
>
> Yours,
> Gary Hayward.
Gary, it looks like you would be interested in the following
kornshell script (conversion to bash is quite easy). If your
brain feels like spaghetti as the moment, perhaps it behooves
you to take advantage of John DuBois's excellent code...
ftp://ftp.armory.com/pub/lib/ksh/AssocArr
Although I have not looked carefully at your code, I would
venture to say there is yet another technique to simulate
associative arrays involving given as integral array indices
the names of variables (the "associative key") which are
pre-typed as integers ("typeset -i var"). This works just fine.
Oh, of course, one could just upgrade to ksh93(1) ;)
=Brian
| |
| Chris F.A. Johnson 2006-10-21, 1:27 am |
| On 2006-10-20, Gazza wrote:
> Hi,
>
> I am almost certain I figured out and implemented a solution to the problem
> below, but I've forgotten what it is, can't find the file(s) on any of my
> computers and my brain is like spaghetti at the moment.
>
> --------- BEGIN BASH SCRIPT ---------
>
> #!/bin/bash
>
> # Arrays of the kind assigned with 'name[number]'
> # and accessed with '${name[number]}' need not be
> # used; there is another way: code to create
> # variables as 'namenumber', where 'name' stays
> # the same, but 'number' is incremented after the
> # creation of the previous one: e.g.:
> # 'fruit1=apple', 'fruit2=orange' and
> # 'fruit3=pear'.
Use eval:
set_avar()
{
eval "ndx=\${${1}_anum:-1}"
eval "${1}_$ndx=\$2"
eval "${1}_anum=$(( $ndx + 1 ))"
}
_get_avar()
{
eval "${3:-_AVAR}=\$${1}_$2"
}
get_avar()
{
_get_avar "$@"
printf "%s\n" "$_AVAR"
}
for f in qw er ty ui op
do
set_avar xx "$f"
done
get_avar xx 3
_get_avar xx 5 q
printf "%s\n" "$q"
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|
|
|
|
|