Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
Registered User
 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
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:

Code:
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

Last edited by luky55; 12-05-2012 at 08:27 AM..
Sponsored Links
    #2  
Old 12-05-2012
Registered User
 
Join Date: Nov 2012
Posts: 33
Thanks: 3
Thanked 1 Time in 1 Post
Take a look at this thread.
Sponsored Links
    #3  
Old 12-05-2012
gary_w's Avatar
Registered User
 
Join Date: Oct 2010
Posts: 446
Thanks: 31
Thanked 96 Times in 88 Posts
I believe you are trying to do something like this. Note you need Korn shell 93::

Code:
$ 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
$

    #4  
Old 12-05-2012
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,452
Thanks: 48
Thanked 364 Times in 336 Posts
The thread referred to by Phunk will not help you.

Here is a simple working example:

Code:
#!/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

The Following User Says Thank You to fpmurphy For This Useful Post:
luky55 (12-05-2012)
Sponsored Links
    #5  
Old 12-05-2012
Registered User
 
Join Date: Dec 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks, the typeset -n or nameref works great
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compound variable in korn shell ZINGARO Shell Programming and Scripting 5 10-03-2007 10:57 AM
compound variable in korn shell ZINGARO Shell Programming and Scripting 0 08-30-2007 11:30 AM
Compound indirect variable references tkrussel UNIX for Advanced & Expert Users 5 08-21-2005 10:03 AM
Trying to use 'compound variable' in a script irina Shell Programming and Scripting 1 01-29-2004 09:33 PM
Trying to use 'compound variable' in a script neemic Shell Programming and Scripting 3 01-16-2004 06:07 AM



All times are GMT -4. The time now is 02:16 PM.