Refering to compound variables with a variable name | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Refering to compound variables with a variable name

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
luky55 luky55 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 13 December 2012, 9:37 AM EST
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
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
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
gary_w gary_w is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 April 2014, 2:21 PM EDT
Posts: 446
Thanks: 32
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
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 21 October 2014, 12:01 PM EDT
Location: /dev/ph
Posts: 4,822
Thanks: 64
Thanked 429 Times in 398 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
luky55 luky55 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 13 December 2012, 9:37 AM EST
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 08:10 AM.