dictの使い方メモ
をテンプレートにして作成
[
Front page
] [
Page list
|
Search
|
Recent changes
|
RSS of recent changes
]
Start:
Tcl8.5からの新しいデータ型のdictについて。~
~
リストと配列の中間みたいな。構造体みたいに扱うリストの要...
***コマンド早引き [#ddbf8502]
,''dict append'' '''varName key ?value ...?''' ,...
,''dict create'' '''?key value ...?''' ,...
,''dict exists'' '''%%%dictionary%%% key ?key ...?''' ,...
,''dict filter'' '''%%%dictionary%%% filterType ...''' ,...
,''dict for'' '''{keyVar valueVar} %%%dictionary%%% scrip...
,''dict get'' '''%%%dictionary%%% ?key key ...?''' ,key...
,''dict incr'' '''varName key ?increment?''' ,keyの...
,''dict info'' '''%%%dictionary%%%''' ,dict...
,''dict keys'' '''%%%dictionary%%% ?pattern?''' ,keyの...
,''dict lappend'' '''varName key ?value ...?''' ,keyの...
,''dict merge'' '''?%%%dictionary%%% ...?''' ,複数...
,''dict remove'' '''%%%dictionary%%% ?key ...?''' ,keyの...
,''dict replace'' '''%%%dictionary%%% ?key value ...?''' ...
,''dict set'' '''varName key ?key ...? value''' ...
,''dict size'' '''%%%dictionary%%%''' ...
,''dict unset'' '''varName key ?key ...?''' ...
,''dict update'' '''varName key varName ?key varName ...?...
,''dict values'' '''%%%dictionary%%% ?pattern?''' ...
,''dict with'' '''varName ?key ...? script''',dictの名前...
-varNameはdict変数の名前で、%%%dictionary%%%は値そのもの...
-get, exists, set, unsetでkeyを複数指定するのはネストされ...
***基本的な使い方 [#t94be0c2]
set d [dict create name toyota]
で作成。必要に応じてlist型と相互変換してるので、[list nam...
あとは普通に上の早引きを見ながらちょちょいと使ってみれば...
set d [dict create]
dict set d name toyota
dict set d code 7203
みたいな。
***ネスト [#k85d37b6]
dictは入れ子にできる。これはdictがarrayと違う大きな要因の...
% set d [dict create a [dict create aa 0] b 1]
a {aa 0} b 1
%
% dict get $d a
aa 0
%
% dict get $d a aa
0
これはaの子のdictのaaにアクセスしてみた感じ。~
もしこれが{a {b0 b1} c}みたいなリストでb1を得たいならlind...
dict set でもネストされたとこにアクセスして値をセットでき...
これはエラー
set d [dict create a 0 b 1]
dict set d c cc 2
~
こうやる。
set d [dict create a 0 b 1]
dict set d c {}
dict set d c cc 2
***便利な使いどころ [#g9d659f7]
arrayの代用。arrayはprocの中からそのまま返せなかったりす...
# 例1 : array get/array setで入出力
proc test {alist} {
array set arr $alist
....
return [array get arr]
}
array set ga {hoge 0 fuga 1}
test [array get ga]
# 例2 : upvarで参照渡しみたいな。
proc test {alist} {
upvar $alist arr
....
}
array set ga {hoge 0 fuga 1}
test ga
~
めんどくさいわけですよ。そこでdict。procからそのまま値を...
proc test {} {
return [dict create hoge 0 fuga 1]
}
puts [test]
やっと人並みになった感じが・・・。つうか今のarrayを廃止し...
***dict filterの練習 [#o7417ebd]
フィルターはkey, value, scriptの三つの種類がある。
dict filter dict key pattern
dict filter dict value pattern
dict filter dict script {keyName valueName} body
% dict filter {aa 0 ab 1 ac 2 ba 3 bb 4 bc 5} key a*
ac 2 aa 0 ab 1
% dict filter {n1 akane n2 aiko n3 ayumi n4 ai} value ai*
n2 aiko n4 ai
% dict filter {toyota 1.15 honda 3.05 nissan -0.71} scri...
toyota 1.15 honda 3.05
***dict updateの練習 [#sc98224a]
set info {name toyota code 7203}
dict update info name nameVar code codeVar {
set nameVar [string toupper $nameVar]
set codeVar ($codeVar)
}
puts $info
name TOYOTA code (7203)
と、まあこの場合、infoのnameがnameVarに、codeがcodeVarに...
~
perを追加してみる例。
set info {name toyota code 7203 price 5280}
dict update info per perVar {
set perVar 15
}
puts $info
name toyota per 15 price 5280 code 7203
あまり良い例じゃないような・・・。まあいいか。~
~
priceを削除してみる例
set info {name toyota code 7203 price 5280}
dict update info price priceVar {
unset priceVar
}
puts $info
name toyota code 7203
最後の書き戻しの時に、マップされた値(この場合priceVar)が...
~
body中でもdictにアクセスすることもできる。priceの2倍の値...
set info {name toyota code 7203 price 5280}
dict update info price2 price2Var {
set price2Var [expr 2 * [dict get $info price]]
}
puts $info
price2 10560 name toyota price 5280 code 7203
***dict withの練習 [#r4514666]
BASICのwithみたいなイメージ?
set info {name toyota code 7203 price 5280}
dict with info {
puts name=$name
puts code=$code
puts price=$price
}
みたいな。書き換えても反映される。~
~
dict updateと同じように、対応した名前が見つからない時は要...
set info {name toyota code 7203 price 5280}
dict with info {
unset name
}
puts $info
price 5280 code 7203
***速度比較 [#g63ab74c]
***コメントをどーぞ [#d011b37e]
- dictはそのままファイルに保存できるのもいいですね。ただ...
- array はそれ自体の配列がとれなかった。それをdictに期待...
- array getもあるし、dictはリスト互換だし、何を言いたいの...
#comment
***リンク [#x5748fb9]
-http://www.tcl.tk/man/tcl8.5/TclCmd/dict.htm
-http://www.tcl.tk/cgi-bin/tct/tip/111.html
-http://www.tcl.tk/cgi-bin/tct/tip/212.html
-[[dict:http://wiki.tcl.tk/5042]]
-[[Dict VS Array Speed:http://wiki.tcl.tk/13826]]
-http://pascal.scheffers.net/software/ (Tcl8.4用のdictラ...
-http://svn.scheffers.net/misc/dict/ (上の作者さんのリポ...
----
[[CategoryTclTk]]
End:
Tcl8.5からの新しいデータ型のdictについて。~
~
リストと配列の中間みたいな。構造体みたいに扱うリストの要...
***コマンド早引き [#ddbf8502]
,''dict append'' '''varName key ?value ...?''' ,...
,''dict create'' '''?key value ...?''' ,...
,''dict exists'' '''%%%dictionary%%% key ?key ...?''' ,...
,''dict filter'' '''%%%dictionary%%% filterType ...''' ,...
,''dict for'' '''{keyVar valueVar} %%%dictionary%%% scrip...
,''dict get'' '''%%%dictionary%%% ?key key ...?''' ,key...
,''dict incr'' '''varName key ?increment?''' ,keyの...
,''dict info'' '''%%%dictionary%%%''' ,dict...
,''dict keys'' '''%%%dictionary%%% ?pattern?''' ,keyの...
,''dict lappend'' '''varName key ?value ...?''' ,keyの...
,''dict merge'' '''?%%%dictionary%%% ...?''' ,複数...
,''dict remove'' '''%%%dictionary%%% ?key ...?''' ,keyの...
,''dict replace'' '''%%%dictionary%%% ?key value ...?''' ...
,''dict set'' '''varName key ?key ...? value''' ...
,''dict size'' '''%%%dictionary%%%''' ...
,''dict unset'' '''varName key ?key ...?''' ...
,''dict update'' '''varName key varName ?key varName ...?...
,''dict values'' '''%%%dictionary%%% ?pattern?''' ...
,''dict with'' '''varName ?key ...? script''',dictの名前...
-varNameはdict変数の名前で、%%%dictionary%%%は値そのもの...
-get, exists, set, unsetでkeyを複数指定するのはネストされ...
***基本的な使い方 [#t94be0c2]
set d [dict create name toyota]
で作成。必要に応じてlist型と相互変換してるので、[list nam...
あとは普通に上の早引きを見ながらちょちょいと使ってみれば...
set d [dict create]
dict set d name toyota
dict set d code 7203
みたいな。
***ネスト [#k85d37b6]
dictは入れ子にできる。これはdictがarrayと違う大きな要因の...
% set d [dict create a [dict create aa 0] b 1]
a {aa 0} b 1
%
% dict get $d a
aa 0
%
% dict get $d a aa
0
これはaの子のdictのaaにアクセスしてみた感じ。~
もしこれが{a {b0 b1} c}みたいなリストでb1を得たいならlind...
dict set でもネストされたとこにアクセスして値をセットでき...
これはエラー
set d [dict create a 0 b 1]
dict set d c cc 2
~
こうやる。
set d [dict create a 0 b 1]
dict set d c {}
dict set d c cc 2
***便利な使いどころ [#g9d659f7]
arrayの代用。arrayはprocの中からそのまま返せなかったりす...
# 例1 : array get/array setで入出力
proc test {alist} {
array set arr $alist
....
return [array get arr]
}
array set ga {hoge 0 fuga 1}
test [array get ga]
# 例2 : upvarで参照渡しみたいな。
proc test {alist} {
upvar $alist arr
....
}
array set ga {hoge 0 fuga 1}
test ga
~
めんどくさいわけですよ。そこでdict。procからそのまま値を...
proc test {} {
return [dict create hoge 0 fuga 1]
}
puts [test]
やっと人並みになった感じが・・・。つうか今のarrayを廃止し...
***dict filterの練習 [#o7417ebd]
フィルターはkey, value, scriptの三つの種類がある。
dict filter dict key pattern
dict filter dict value pattern
dict filter dict script {keyName valueName} body
% dict filter {aa 0 ab 1 ac 2 ba 3 bb 4 bc 5} key a*
ac 2 aa 0 ab 1
% dict filter {n1 akane n2 aiko n3 ayumi n4 ai} value ai*
n2 aiko n4 ai
% dict filter {toyota 1.15 honda 3.05 nissan -0.71} scri...
toyota 1.15 honda 3.05
***dict updateの練習 [#sc98224a]
set info {name toyota code 7203}
dict update info name nameVar code codeVar {
set nameVar [string toupper $nameVar]
set codeVar ($codeVar)
}
puts $info
name TOYOTA code (7203)
と、まあこの場合、infoのnameがnameVarに、codeがcodeVarに...
~
perを追加してみる例。
set info {name toyota code 7203 price 5280}
dict update info per perVar {
set perVar 15
}
puts $info
name toyota per 15 price 5280 code 7203
あまり良い例じゃないような・・・。まあいいか。~
~
priceを削除してみる例
set info {name toyota code 7203 price 5280}
dict update info price priceVar {
unset priceVar
}
puts $info
name toyota code 7203
最後の書き戻しの時に、マップされた値(この場合priceVar)が...
~
body中でもdictにアクセスすることもできる。priceの2倍の値...
set info {name toyota code 7203 price 5280}
dict update info price2 price2Var {
set price2Var [expr 2 * [dict get $info price]]
}
puts $info
price2 10560 name toyota price 5280 code 7203
***dict withの練習 [#r4514666]
BASICのwithみたいなイメージ?
set info {name toyota code 7203 price 5280}
dict with info {
puts name=$name
puts code=$code
puts price=$price
}
みたいな。書き換えても反映される。~
~
dict updateと同じように、対応した名前が見つからない時は要...
set info {name toyota code 7203 price 5280}
dict with info {
unset name
}
puts $info
price 5280 code 7203
***速度比較 [#g63ab74c]
***コメントをどーぞ [#d011b37e]
- dictはそのままファイルに保存できるのもいいですね。ただ...
- array はそれ自体の配列がとれなかった。それをdictに期待...
- array getもあるし、dictはリスト互換だし、何を言いたいの...
#comment
***リンク [#x5748fb9]
-http://www.tcl.tk/man/tcl8.5/TclCmd/dict.htm
-http://www.tcl.tk/cgi-bin/tct/tip/111.html
-http://www.tcl.tk/cgi-bin/tct/tip/212.html
-[[dict:http://wiki.tcl.tk/5042]]
-[[Dict VS Array Speed:http://wiki.tcl.tk/13826]]
-http://pascal.scheffers.net/software/ (Tcl8.4用のdictラ...
-http://svn.scheffers.net/misc/dict/ (上の作者さんのリポ...
----
[[CategoryTclTk]]
Page:
HTML convert time: 0.003 sec.