Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ksh associate arrays

I have a script that needs to use associative arrays. Being new to ksh, I am unable to find anywhere that ksh supports associative arrays. When I try to use regular array syntax and assign, I get an error that index cannot be that big. Does ksh support associative arrays? If not, what is the alternative solution?

I need to do the following:

${array[$name]}=value

and later in the code, to read a value for

${array[$name]}

I have about 2000 values to be stored and read from the array every time this script runs.

Unfortunately, I cannot use perl due to the extent of legacy modules to be included inside the script. Appreciate any help, tips or techniques.

like image 302
Kiran Avatar asked Dec 09 '10 04:12

Kiran


People also ask

What is associative array in shell script?

An array variable is used to store multiple data with index and the value of each array element is accessed by the corresponding index value of that element. The array that can store string value as an index or key is called associative array.

How to declare an array in Korn shell?

To define an associative array in the Korn shell, we use the command "typeset -A" followed by the name of the array we are creating. Once this is done, we can start assigning elements to the array.

What is the difference between the associative array and multidimensional array?

Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.

What is Korn shell known as?

The Korn shell is the UNIX shell (command execution program, often called a command interpreter ) that was developed by David Korn of Bell Labs as a comprehensive combined version of other major UNIX shells.


1 Answers

The ksh typeset command is used to declare an associative array.

$ typeset -A age
$ age[bob]=42
$ age[alice]=31
$ print ${age[bob]}
42
like image 60
dwarring Avatar answered Sep 28 '22 12:09

dwarring