'Associative container'에 해당되는 글 1건

  1. 2015.11.14 Bash - Associative container 이용
Linux2015. 11. 14. 16:22

문제:

Bash script에서 (C++의 map과 비슷한) associative container를 이용하는 방법.

 

 

단계 1:

Associative container 변수 선언: 

declare -A cycles

  • -A 이용.
  • cycles는 예시로 쓰인 변수 이름.

 

 

단계 2:

Element들을 insert (cycles[key] = value): 

cycles[barnes]=504783668

cycles[blacksholes]=27964256

 

 

단계 3:

Element access:

echo ${cycles[barnes]}

  • ${cycles[key]}
  • 또한 key 자리에 "key"가 들어가도 됨.
  • 위의 echo 결과는 물론:
    • 504783668

 

 

추가 내용:

여러 key-value들을 한 번에 넣을 때:

declare -A cycles=([barnes]=504783668 \

                   [blackscholes]=27964256 \

                   [bodytrack]=371969846)

 

여러 key-value들을 추가로 넣을 때 (+= 이용):

cycles+=([bodytrack]=371969846 \

         [cholesky]=213506158)

 

참고로, bash에서 array는 (elem0 elem1 ..) 임.

  • Element들 사이에 comma (,)가 없음에 주의.
  • Associative array에서 (())를 이용하는 것은 아마도 array에서 따온 것이 아닐까?

 

 

참고 문헌:

'Linux' 카테고리의 다른 글

fsck - Root partition에 fsck 하기  (0) 2015.12.18
a2ps - Source code 예쁘게 인쇄  (0) 2015.10.11
Posted by topazus