UNDER KSH, HOW TO MAKE VARIABLE STRING VARIABLE NAME REFERENCES

ITEM: RTA000052265



                                                                                
                                                                                
                                                                                
QUESTION:                                                                       
I am using ksh and would like to use string variables that can                  
be modified.                                                                    
                                                                                
Basically, what I am trying to do is this:                                      
                                                                                
        A1="string 1"                                                           
        A2="string 2"                                                           
        C is a "common" string variable                                         
                                                                                
I would like to use C as a common string variable that could be                
set within a loop to pick up the various Ax values.  As an example,             
                                                                                
            D = 1                                                               
            while (D < 5)                                                       
            do                                                                  
                C=$A$D                                                          
                etc                                                             
                                                                                
Ignore syntax here.  Anyway, when I try to do this I get back a                 
ksh message saying "1 not defined" for the first value and so on.               
                                                                                
Is there a way to do this?  I have a 1400 line script that could                
be squashed down to one sixth it's size if I could make the variable            
references variable.                                                           
                                                                                
---------- ---------- ---------- --------- ---------- ----------                
A: The best way to accomplish what you desire is to use Korn Shell's            
   array feature.  I have included some sample source code below that           
   demonstrates the use of ksh arrays:                                          
                                                                                
#¢/bin/ksh                                                                      
                                                                                
string="This is a big really really long string"                                
                                                                                
set -A array2 This is another way to set an array                               
                                                                                
array1.1.=This                                                                  
array1.2.=is                                                                   
array1.3.=an                                                                    
array1.4.=example                                                               
array1.5.=of                                                                    
array1.6.=arrays                                                                
array1.7.=in                                                                    
array1.8.=action                                                                
                                                                                
let x=0                                                                         
                                                                                
for word in $string                                                             
do                                                                              
  let x=x+1                                                                     
  array3.x.=$word                                                               
done                                                                           
                                                                                
print "\n\n\nStrings...\n"                                                      
                                                                                
print "Array array1:" ${array1.*.}                                              
                                                                                
print "Array array2:" ${array2.*.}                                              
                                                                                
print "Array array3:" ${array3.*.}                                              
                                                                                
print "\nArray2 has" ${#array2.*.} "elements.\n"                                
                                                                                
while .. $x > 0 ..                                                              
do                                                                              
  printf ' d:  9s  9s  9s\n' $x ${array1.x.} ${array2.x-1.} ${array3.x.}       
  let x=x-1                                                                     
done                                                                            
                                                                                
  Unfortunately, there will be much of this script which will not               
  translate including the following:                                            
                                                                                
  Left Curly Bracket   '{'   Right Curly Bracket   '}'                          
  Left Square Bracket  '.'   Right Square Bracket  '.'                          
  Backslash            '\'   Percent               ' '                          
                                                                                
  If the result is too unreadable, I will be happy to fax this code to          
  you.  Please reference 'Learning the Korn Shell' by Bill Rosenblatt           
  or 'The Kornshell Command and Programming Language' by Morris I. Bolsky       
  and David G. Korn for additional information                                 
                                                                                
---------- ---------- ---------- --------- ---------- ----------                
QUESTION:                                                                       
I got a great tip from my customer on a command called eval.                    
An example of it's use is:                                                      
                                                                                
A1="This is A1"                                                                 
A2="This is A2"                                                                 
                                                                                
for X in 1 2                                                                    
do                                                                              
  eval C='$A'$X  # $X will be evaluated but $A won't be until                   
  echo $C        # C is "accessed" because it is quoted                         
done                                                                           
                                                                                
This method avoids the use of arrays which ksh has a minimum                    
allocation of 1024 (according to one book I read), and I find                   
it pretty easy to use.                                                          
                                                                                
---------- ---------- ---------- --------- ---------- ----------                
                                                                                
                                                                                
This item was created from library item Q675899      FFFKG                      
                                                                                
Additional search words:                                                        
FFFKG IX JAN95 KSH LPPS NAME NAMING OZNEW REFERENCE REFERENCES RISCO            
RISCSYSTEM SOFTWARE STRING SUPT VARIABLE                                        
                                                                               


WWQA: ITEM: RTA000052265 ITEM: RTA000052265
Dated: 01/1995 Category: RISCO
This HTML file was generated 99/06/24~12:43:20
Comments or suggestions? Contact us