From: sealo on
Hello, dear all,
I find the array in the bash only allow the index type to be NUMBER,
such as

array[0]="A";
array[1]="B";

But I have the requirement that to map the string to string, but this
was forbidden in the bash array. What I want like this:

# array["AA"]=1;
# array["BB"]=2;
# array["CC"]=3;

# echo ${array[@]};
1 2 3

# array["CC"]=4;
# echo ${array[@]};
1 2 4

But actually, the output was not this result.

Do you have some idea?

From: Bill Marcum on
On 30 Jan 2007 21:02:18 -0800, sealo
<seahalo(a)gmail.com> wrote:
>
>
> Hello, dear all,
> I find the array in the bash only allow the index type to be NUMBER,
> such as
>
> array[0]="A";
> array[1]="B";
>
> But I have the requirement that to map the string to string, but this
> was forbidden in the bash array. What I want like this:
>
> # array["AA"]=1;
> # array["BB"]=2;
> # array["CC"]=3;
>
> # echo ${array[@]};
> 1 2 3
>
> # array["CC"]=4;
> # echo ${array[@]};
> 1 2 4
>
> But actually, the output was not this result.
>
> Do you have some idea?
>
Use zsh or awk. All *ix systems have awk.

--
The relative importance of files depends on their cost in terms of the
human effort needed to regenerate them.
-- T. A. Dolotta
From: Chris F.A. Johnson on
On 2007-01-31, sealo wrote:
> Hello, dear all,
> I find the array in the bash only allow the index type to be NUMBER,
> such as
>
> array[0]="A";
> array[1]="B";
>
> But I have the requirement that to map the string to string, but this
> was forbidden in the bash array. What I want like this:
>
> # array["AA"]=1;
> # array["BB"]=2;
> # array["CC"]=3;
>
> # echo ${array[@]};
> 1 2 3
>
> # array["CC"]=4;
> # echo ${array[@]};
> 1 2 4
>
> But actually, the output was not this result.
>
> Do you have some idea?

Can you use ksh93, which has associative arrays?

If not, you can simulate arrays, e.g.:

setarray() { #@ USAGE: setarray varname val ...
setarray_name=$1
shift
set -f
set -- "$@"
set +f ## Should really check that f was previously set
n=0
for set_var
do
setv ${setarray_name}_$n "$set_var"
n=$(( $n + 1 ))
shift
done
}

setv() { #@ USAGE: setv varname value
setv_var=$1
local IFS="" ## not portable; adjust for your shell if necessary
eval "$setv_var=\$2"
}

_getv() { #@ USAGE: _getv varname_subscript [var]
eval "_GETV=\$$1"
case $# in
2) eval "$2=\$_GETV" ;;
esac
}

getv() { #@ USAGE: getv varname
eval echo "\$$1"
}

To populate an array named q:

setarray q qw er ty ui op

To set an individual element:

n=2
setv q_$n qwerty

To print the value of one element:

getv q_$n

To put the value of an array element into another variable:

_getv q_$n value

--
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
From: Stephane CHAZELAS on
2007-01-30, 21:02(-08), sealo:
> Hello, dear all,
> I find the array in the bash only allow the index type to be NUMBER,
> such as
>
> array[0]="A";
> array[1]="B";
>
> But I have the requirement that to map the string to string, but this
> was forbidden in the bash array. What I want like this:
>
> # array["AA"]=1;
> # array["BB"]=2;
> # array["CC"]=3;
>
> # echo ${array[@]};
> 1 2 3
>
> # array["CC"]=4;
> # echo ${array[@]};
> 1 2 4
>
> But actually, the output was not this result.
[...]

If you need associative arrays, you probably need a programming
language, not a shell, or the shell only to call that
programming language interpreter for interpreted languages.

Depending on what you want to do with those arrays, awk or perl
may come handy.

--
St�phane
From: William James on
On Jan 31, 7:31 am, Stephane CHAZELAS <this.addr...(a)is.invalid> wrote:
> 2007-01-30, 21:02(-08), sealo:
>
> > Hello, dear all,
> > I find the array in the bash only allow the index type to be NUMBER,
> > such as
>
> > array[0]="A";
> > array[1]="B";
>
> > But I have the requirement that to map the string to string, but this
> > was forbidden in the bash array. What I want like this:
>
> > # array["AA"]=1;
> > # array["BB"]=2;
> > # array["CC"]=3;
>
> > # echo ${array[@]};
> > 1 2 3
>
> > # array["CC"]=4;
> > # echo ${array[@]};
> > 1 2 4
>
> > But actually, the output was not this result.
>
> [...]
>
> If you need associative arrays, you probably need a programming
> language, not a shell, or the shell only to call that
> programming language interpreter for interpreted languages.
>
> Depending on what you want to do with those arrays, awk or perl
> may come handy.
>
> --
> Stéphane

Try Ruby.