ハッシュテーブルとは、高速にデータの関連付けをするデータ構造である。
キーと値を関連付けたりキーか値を取り出したりすることが簡単にできるそうな。
;; ハッシュテーブルを作成する (setq hash (make-hash-table :test 'equal)) ; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ()) ;; ハッシュテーブルペアを追加する (puthash "one" 1 hash) ; => 1 (puthash "two" 2 hash) ; => 2 (puthash "three" 3 hash) ; => 3 hash ; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ("one" 1 "two" 2 "three" 3)) ;; キーから値を取り出す (gethash "one" hash) ; => 1 ;; ペアを削除する (remhash "one" hash) ; => nil hash ; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data ( "two" 2 "three" 3)) ;; リテラルを使う (setq hash #s(hash-table test equal data (one 1 two 2 three 3))) ; => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (one 1 two 2 three 3)) ;; loopを使う ;; キーのリストを得る (require 'cl) (loop for k being hash-key in hash collect k) ; => (one two three) ;; 値のリストを得る (loop for v being hash-value in hash collect v) ; => (1 2 3) ;; キーとリストのリストを得る (loop for k being hash-key in hash using (hash-values v) collect (cons k v)) ; => ((one . 1) (two . 2) (three . 3))
P161
- 作者: るびきち
- 出版社/メーカー: 技術評論社
- 発売日: 2011/11/26
- メディア: 単行本(ソフトカバー)
- 購入: 5人 クリック: 220回
- この商品を含むブログを見る