2012-12-12 2 views
1

나는, 나는 세 가지 클래스에 해당하는 숫자의 순서, 즉이 수행 "n_vars"매번 따라, 그래서 실제로 할당 동적 다차원 변수

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

같은 라인을 가지고 할 XML을해야합니까 코드 1, CODE2, CODE3 및 이러한 클래스 각각에 대해 나는 스마트 떠들썩한 파티에 라인을 (좋아, 이것이 내가 알고 ;-) 읽고 "n_vars"를 할당 얻을 수있는 방법 .. 각각에 대해 "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