Refering to compound variables with a variable name

Hello,

Here is my problem using KSH
I have a set of compound variables, let say cmp_var1 cmp_var2
The names of these variables are stored in an indexed array.
How can I access the subfields of these compound variables ?
I tried:

set -A cmp_varnames=(cmp_var1 cmp_var2)
for cmp in ${cmp_varnames
[*]}
do
eval ${${cmp}.field}
done

That does not work, I tried a few other things, no luck.
Thanks for helping

Take a look at this thread.

I believe you are trying to do something like this. Note you need Korn shell 93::

$ cat x
#!/usr/dt/bin/dtksh

## Define Compound variables.
cmp_var1=( code=10 desc="hello" )
cmp_var2=( code=20 desc="world" )

## Create array of compound variables.
set -A cmp_varnames cmp_var1 cmp_var2

## Print the descriptions.
for cmp in ${cmp_varnames
[*]}
do
  # Use a name reference instead of eval.
  typeset -n mydesc="$cmp.desc"
  print $mydesc
done

exit 0
$ ./x
hello
world
$

The thread referred to by Phunk will not help you.

Here is a simple working example:

#!/bin/ksh93

typeset -C cmp_var1
cmp_var1.field=date

typeset -C cmp_var2
cmp_var2.field=whoami

set -A cmp_varnames cmp_var1 cmp_var2

for cmp in ${cmp_varnames[*]}
do
   nameref my=$cmp.field
   $my
done                                   
1 Like

Thanks, the typeset -n or nameref works great