简体   繁体   中英

KSH: Using an array location on another array reference

I am using mksh ( mksh-50f-5.1.x86_64 ) and trying to use an element in an array as the string name in another array.

This is a small example of what I am having difficulty with.

Each array ( Array_01 -> Array_04 ) has 10 random elements.

There is an array ( ArrayNames ) with all 4 array names in it.

The outer loop will take one name from the ( ArrayNames ) but will then fail while trying to print each element in each ( Array_0? ).

#!/bin/ksh

set -A Array_01 `shuf -i 1-100 -n 10`
set -A Array_02 `shuf -i 1-100 -n 10`
set -A Array_03 `shuf -i 1-100 -n 10`
set -A Array_04 `shuf -i 1-100 -n 10`
print ${Array_01[*]}

set -A ArrayNames Array_01 Array_02 Array_03 Array_04
integer i=0,j=0
while (( i < ${#ArrayNames[*]} ))    
do
  print "Array Name = [ ${ArrayNames[i]} ]"
  while (( j < ${#**`echo ${ArrayNames[i]}`**[*]} ))
  do
    print ${`echo ${ArrayNames[j]}`[j]}
    (( j = j + 1 ))
  done
  (( j = 0 ))
  (( i = i + 1 ))
done
# ./sample.ksh    

53 4 12 99 22 95 47 21 77 86

Array Name = [ Array_01 ]

./sample.ksh[29]: ${#$(echo ${ArrayNames[i]})[*]} ": bad substitution

[ Update - Tried a variant. ]

!/bin/ksh

set -A TwoElements 7 8
set -A SixElements 1 2 3 4 5 6

set -A ArrayNameList TwoElements SixElements

integer i=0

print "STEP01: Contents of array TwoElements = [ ${TwoElements[*]} ]"
print "STEP02: Contents of array SixElements = [ ${SixElements[*]} ]"

while (( i < ${#ArrayNameList[*]} ))
do
  CurrentArray=$( print ${ArrayNameList[${i}]} )
  print "STEP03: Attempting to dump array [ $( print ${ArrayNameList[${i}]} ) ]"
  print "STEP04: ${CurrentArray}"
  print "STEP05: ${${CurrentArray}[*]}"
  (( i = i + 1 ))
done

This is what it printed...

STEP01: Contents of array TwoElements = [ 7 8 ]

STEP02: Contents of array SixElements = [ 1 2 3 4 5 6 ]

STEP03: Attempting to dump array [ TwoElements ]

STEP04: TwoElements

./sample.ksh[20]: ${${CurrentArray}[*]}": bad substitution

Missing is STEP05 which should be dumping the contents of the first array name "TwoElements".

I don't know mksh. But this is what I would do in ksh93.

#!/bin/ksh

set -A Array_01 `shuf -i 1-100 -n 10`
set -A Array_02 `shuf -i 1-100 -n 10`
set -A Array_03 `shuf -i 1-100 -n 10`
set -A Array_04 `shuf -i 1-100 -n 10`
print ${Array_01[*]}

set -A ArrayNames Array_01 Array_02 Array_03 Array_04
for (( i=0; i < ${#ArrayNames[*]}; i++ ))
do
  print "Array Name = [ ${ArrayNames[$i]} ]"
  nameref temparray=${ArrayNames[$i]}
  for (( j=0; j < ${#temparray[*]}; j++ ))
  do
    print ${temparray[$j]}
  done
done

The output gives :

10 73 77 61 74 39 90 45 2 75
Array Name = [ Array_01 ]
10
73
77
61
74
39
90
45
2
75
Array Name = [ Array_02 ]
50
37
9
8
47
26
82
55
11
99
Array Name = [ Array_03 ]
13
65
66
78
15
85
96
4
33
76
Array Name = [ Array_04 ]
7
2
87
86
100
76
47
30
75
31

Hope it could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM