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.
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.
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.
Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.
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.
The ksh typeset
command is used to declare an associative array.
$ typeset -A age
$ age[bob]=42
$ age[alice]=31
$ print ${age[bob]}
42
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With