2012-12-12 68 views
1

我有XML,這確實有像分配動態多維變量

#<pdfrwt "2"> n_vars code(1) ... code1(2).... code1(n_vars) code2(1) ... code2(n_vars) code3(1) ... code3(n_vars)</pdfrwt> 

線因此,實際上,這取決於在「n_vars」每一次,我有一個數字序列對應於三類,即code1,code2,code3和每一個這些類我得到每個「n_vars」條目..所以,我怎麼能巧妙地閱讀bash中的行(好吧,我知道;-),並得到分配一個「n_vars」多維變量,即像

output1[1]=code1(1) 
output1[2]=code1(2) 
... 
output1[n]=code1(n) 
and likewise 

output2[1]=code2(1) 
.. 
output2[1]=code2(1) 

在此先感謝

Alex

回答

1

您可以將整行讀入數組,然後對數組進行切片。

# hdr gets the first field, n_vars gets the second, and rest gets 
# the remaining values 
read hdr n_vars rest < file 

# Split rest into a proper array 
array=($rest) 
# Copy the desired slices from array into the three output arrays. 
output1=("${array[@]:0:n_vars}") 
output2=("${array[@]:n_vars:n_vars}") 
output3=("${array[@]:2*n_vars:n_vars}") 

如何訪問值的一些例子:

echo ${output1[0]} # The first element of output1 
echo ${output2[3]} # The fourth element of output2 
echo ${output3[$#output3]} # The last element of output3 
echo "${output1[@]}" # All the elements of output1 
+0

嗨 非常感謝你...所以,我怎麼能真正在屏幕上讀取/打印多維「輸出」變量? $ output [0] [1]當然不起作用... 再次感謝 – Alex