(for Internet Explorer)
辞書 (Dictionary)
Function  Dict( Elems as array ) as Dictionary
辞書を生成します。
(src)
Elems の奇数番目の要素は、キー、偶数番目の要素は、アイテムになります。
キーは文字列のみですが、アイテムはオブジェクトなども指定できます。
サンプル
Set  dic = Dict(Array( "Key1","Item1", "Key2","Item2" ))
Set  dic = CreateObject( "Scripting.Dictionary" )
dic.Item( "Key1" ) = "Item1"
dic.Item( "Key2" ) = "Item2"
上記は下記と同じ処理内容です
→ T_Dic.vbs # T_Dict
テスト
関連
→ JSON (JavaScript Object Notation)
→ DicArray
Set  obj = Dict(Array( "Attr1", 123, "Attr2", "ABC" ))
echo  obj("Attr1") & obj("Attr2")
サンプル
Function  DicTable( DataArray as array ) as array of Dictionary
テーブルのように使う、辞書の配列を生成します。
関連
(src)
サンプル
  For Each t  In DicTable( Array( _
     "Item",  "Price",  "Number",   Empty, _
     "Book",     2000,         2, _
     "Pen",       100,        12, _
     "Eraser",     60,        10 ) )

    echo  t("Item") &" : "& t("Price") * t("Number")
  Next
1行目は、辞書のキー(属性名)を並べ、行末は Empty にします。
2行目以降は、辞書のアイテム(値)を並べます。
上記を実行した結果の出力は、下記のようになります。
Book : 4000
Pen : 1200
Eraser : 600
→ JSON (JavaScript Object Notation)
辞書の内容を見るには、echo o で出力するか、?GetEchoStr( t )
をイミディエイトウィンドウで実行します。
?GetEchoStr( t )
"Item=Book
Price=2000
Number=2"
→ T_Dic.vbs # T_Dict
テスト
Function  JoinDicTable( DicTable1 as array of Dictionary,  KeyField as string,
                        DicTable2 as array of Dictionary ) as array of Dictionary
2つのテーブル(辞書の配列)を、指定のフィールドが一致する行で、結合します。
サンプル
table1 = DicTable( Array( _
   "Item",  "Price",  Empty, _
   "Book",     2000, _
   "Pen",       100, _
   "Eraser",     60  ) )

For Each t  In JoinDicTable( table1, "Item", Array( _
   "Item",  "Number",   Empty, _
   "Book",         2, _
   "Pen",         12, _
   "Eraser",      10 ) )

  echo  t("Item") &" : "& t("Price") * t("Number")
Next
【引数】
DicTable1
KeyField
テーブル(辞書の配列)
比較するフィールド名(辞書のキー)
DicTable2
テーブル(辞書の配列)
結合後のテーブル(辞書の配列)
返り値
(src)
For Each t  In DicTable( Array( _
   "Item",  "Price",  "Number",   Empty, _
   "Book",     2000,         2, _
   "Pen",       100,        12, _
   "Eraser",     60,        10 ) )

  echo  t("Item") &" : "& t("Price") * t("Number")
Next
上記の結合をした結果は、下記と同じです。
Item
Item
Item
JoinDicTable
関連
→ SQL の JOIN の詳細
KeyField を持ち、KeyField が Empty ではない行について、SQL の FULL OUTER JOIN
演算を行います。
テスト
→ T_Dic.vbs # T_DicTable
Function  DicItem( Dic as Dictionary, Key as string ) as variant
登録されていないときも、新規に登録しない、
です。
dic.Item("key1")
DicItem( dic, "key1" )
Dictionary::Item では
DicItem では
登録されていないときは、Empty を返します。
(src)
テスト
→ T_Dic.vbs # T_GetDicItem
Sub  GetDicItem( Dic as Dictionary, Key as string, out_Item as variant )
  GetDicItem  dic, "key1", item  '//[out] item
  If IsEmpty( item ) Then _
    Set item = new ItemClass : item.Name = "key1"
登録されていない Key を指定したときは、out_Item = Empty になります。
このとき、値が Empty の要素を Dic に追加しません。
(src)
Empty でもオブジェクトでも取得できる
です。
【引数】
Dic
Key
辞書
辞書のキー
out_Item
(出力)辞書のアイテム
ファイル:
vbslib.vbs
テスト
→ T_Dic.vbs # T_GetDicItem
サンプル
Function  GetDicItemAsArrayClass( Dic as dictionary, Key as string ) as ArrayClass
辞書のアイテムにある ArrayClass で参照します。 アイテムが無ければ ArrayClass を作成します。
【引数】
Desc
Key
辞書
辞書のキー
返り値
辞書の要素である ArrayClass のオブジェクト
ファイル:
vbslib.vbs
(src)
  Dim  dic, arr

  Set dic = CreateObject( "Scripting.Dictionary" )
  Set arr = GetDicItemAsArrayClass( dic, "key1" )
テスト
→ T_Dic.vbs # T_GetDicItem
サンプル
dic, "key1"
Function  DicItemOfItem( Dic as Dictionary, Key as string ) as variant
辞書の Item が $ か % で囲んだ変数なら、それを Key とした Item を返します。
  Set  dic = CreateObject( "Scripting.Dictionary" )
  dic.Item( "Key1" ) = "$Key3"
  dic.Item( "Key2" ) = "%Key3%"
  dic.Item( "Key3" ) = "Item3"

  Assert  DicItemOfItem( dic, "Key1" ) = "Item3"
  Assert  DicItemOfItem( dic, "Key2" ) = "Item3"
  Assert  DicItemOfItem( dic, "Key3" ) = "Item3"
サンプル
辞書の Item が % か  で囲んだ変数ではないなら、辞書の Item をそのまま返します。
(src)
→ T_Dic.vbs # T_DicItemOfItem
テスト
Function  Dic_addNewObject( Dic as Dictionary,
    ClassName as string, ObjectName as string, IsDuplicate as boolean ) as ClassName
指定した名前を付けたオブジェクトを生成して、辞書に追加します。
【引数】
Dic
ClassName
辞書。 キーが ObjectName の要素が追加される
クラス名
(src)
ObjectName
オブジェクトの名前、辞書のキーになります。
返り値
Name プロパティが ObjectName のオブジェクト
サンプル:
  Set objs = CreateObject( "Scripting.Dictionary" )
  Set o = Dic_addNewObject( objs, "ClassA", "ObjectA", True )

Class ClassA
  Public  Name
End Class
すでに同じ名前のオブジェクトが辞書にあるときは、IsDuplicate = True なら追加しないで、
辞書に入っていたものを返します。 IsDuplicate = False なら E_AlreadyExist エラーになります。
辞書に無いときは、オブジェクトを生成します。
IsDuplicate
False = すでに辞書にあるときはエラーにする
→ T_Dic.vbs [T_DicAddObj]
テスト
関連
ClassName に指定したクラスに、Name プロパティ、またはメンバー変数が必要です。
内部で
を使っているので、new_ClassA 関数の定義が必要です。
g_Vers("CutPropertyM") = False

Class ClassA
  Public  m_Name
End Class
g_Vers("CutPropertyM") = False にすると、Name の代わりに m_Name を使います。
Sub  Dic_addElem( in_out_Dic as Dictionary, Key as string, Item as variant )
辞書の要素を設定します。
【引数】
in_out_Dic
Key
(入出力) 編集する辞書
キー
Item
アイテム
in_out_Dic.Item( Key ) = Item とほぼ同じですが、違いは下記のとおりです。
・Item がオブジェクトかどうかで、Set を記述するかどうかを区別する必要がありません。
・Item が Empty のときは、in_out_Dic からキーを削除します。
(src)
→ T_Dic.vbs # T_DicAdd
テスト
関連
Sub  Dic_add( in_out_Dic as Dictionary, PlusDic as Dictionary )
辞書を加算します。
【引数】
in_out_Dic
PlusDic
(入出力) 加算される辞書
加算する内容が入っている辞書
(src)
PlusDic のアイテムが Empty のキーは、in_out_Dic から削除されます。
値が Empty の変数を in_out_Dic に指定すると、内部で辞書を生成します。
→ T_Dic.vbs # T_DicAdd
テスト
関連
  Set  dic  = Dict(Array( "Key1","Item1", "Key2","Item2" ))
  Dic_add  dic, Dict(Array( "Key2","ITEMS", "Key3","Item3" ))  '//[out] dic
  Assert  dic.Count = 3
  Assert  dic.Item( "Key1" ) = "Item1"
  Assert  dic.Item( "Key2" ) = "ITEMS"
  Assert  dic.Item( "Key3" ) = "Item3"
サンプル
Sub  Dic_sub( out_Dic as Dictionary, WholeDic as Dictionary, BaseDic as Dictionary,
              CompareFunc as function, CompareFuncParam as variant )
辞書を減算します。
【引数】
out_Dic
WholeDic
(出力) WholeDic から BaseDic を減算した結果の辞書
BaseDic に out_Dic を加算した結果と同一の内容が入った辞書
(src)
BaseDic に out_Dic を加算(Dic_add)した結果が、WholeDic になるような out_Dic を取得します。
out_Dic に、値が Empty の変数を指定すると、Dic_sub の内部で生成した辞書が取得できます。

同じキーのアイテムを CompareFunc した結果、等価でなければ、out_Dic にコピーされます。
辞書のアイテムがオブジェクトのときは、WholeDic のアイテムと同一のオブジェクトを指します。
BaseDic にあって WholeDic にないキーは、out_Dic にもできますが、アイテムは Empty と
なります。
BaseDic
Dic_add で加算する前と同一の内容が入った辞書
  Dic_sub  dic, _
    Dict(Array( "Key1","Item1", "Key2","ITEMS", "Key3","Item3" )), _
    Dict(Array( "Key1","Item1", "Key2","Item2", "Key4","Item4" )), _
    Empty, Empty
    '//[out] dic

  '// dic = Dict(Array( "Key2","ITEMS", "Key3","Item3", "Key4",Empty ))
サンプル:
CompareFunc
CompareFuncParam
通常 Empty、CompareFunc の第3引数
、Empty =
通常 Empty、
→ T_Dic.vbs # T_DicAdd
テスト
サンプル:
  Dic_sub  dic, whole_dic, base_dic, Empty, Empty '//[out] dic
Sub  Dic_addFromArray( in_out_Dic as Dictionary, Keys as array of string, Items as variant )
配列を使って、辞書に追加します。
【引数】
in_out_Dic
Keys
(入出力) 加算される辞書
キーとなる文字列が入っている配列
Items
キーに対応する値、または、その配列
サンプル
Set  dic = CreateObject( "Scripting.Dictionary" )
Dic_addFromArray  dic, Array( "key1", "key2" ), True
上記は下記と同じ内容です。
Set  dic = CreateObject( "Scripting.Dictionary" )
dic.Item( "key1" ) = True
dic.Item( "key2" ) = True
dic.Item( "key1" ) = 1
dic.Item( "key2" ) = 2
上記は下記と同じ内容です。
Dic_addFromArray  dic, Array( "key1", "key2" ), Array( 1, 2 )
サンプル
→ T_Dic.vbs # [T_DicArray]
テスト
(src)
Sub  DicToArr( Dic as Dictionary, out_Arr as array of DicElem )
Dictionary を配列に変換します。 配列の要素は DicElem 型になります。
【引数】
Dic
out_Arr
Dictionary
(出力) DicElem 型の配列。
Dic の内容を Arr に変換すると、デバッガで内容を見ることができます。
(Visual Studio 2005)
(src)
Class  DicElem
  Public  Key
  Public  Item
  Public Property Get/Let  Name  '// Key と同じ
  Public Sub  Init( a_Key, a_Item )
End Class
サンプル:
  DicToArr  dic, arr  '//[out] arr
関連
(src)
Sub  DicKeyToArr( Dic as Dictionary, out_Array as array of string )
Dictionary のキーを配列に変換します。
(src)
DicKeyToCSV
Function  DicKeyToCSV( Dic as Dictionary ) as string
Dictionary のキーを CSV に変換します。
(src)
サンプル
Set  dic = CreateObject( "Scripting.Dictionary" )
dic.Item( "Key1" ) = "Item1"
dic.Item( "Key2" ) = "Item2"
Assert  DicKeyToCSV( dic ) = "Key1,Key2"
DicKeyToCSV
ファイル:
vbslib.vbs
テスト
→ T_Dic.vbs # [T_DicKeyToCSV]
Dic に要素が1つも無いときは、"" を返します。
Sub  DicItemToArr( Dic as Dictionary, out_Array as array of variant )
Dictionary のアイテムを配列に変換します。
(src)
Sub  QuickSort_fromDic( Dic as dictionary, out_Arr as array of object,
                        CompareFunc as function, Param as variant )
【引数】
Dic
ソートするデータが入った辞書。(辞書はソートされません)
辞書に入っているデータ(Dictionary::Item)をクイックソートします。
(出力) ソートした結果の配列。配列の要素はオブジェクト
out_Arr
CompareFunc
Param
compare_func の第3引数
(src)
サンプル
  QuickSort_fromDic  dic, arr, GetRef("StdCompare"), Empty  '//[out]arr
関連
Sub  ShakerSort_fromDic( dic as Dictionary, out_arr as object array,
        sign as integer, compare_func as function, param as variant )
【引数】
dic
ソートするデータが入った辞書。(辞書はソートされません)
辞書に入っているデータ(Dictionary::Item)をシェーカーソートします。
ソートした結果の配列。配列の要素はオブジェクト。
out_arr
compare_func
param
compare_func の第3引数
参考
sign
正の数=dic の順がベース、負の数=dicの逆順がベース(*1)
(*1) sign 引数の説明
dic.Item の For ループの順番どおりの配列を作成してからシェーカーソートを開始
するか、逆順の配列を作成してからシェーカーソートを開始するかを指定します。
sign < 0 のときでも、compare_func は、sign > 0 のときの compare_func の返り値の
マイナスを返すようにしてください。
(src)
Sub  ShakerSort_fromDicKey( Dic as Dictionary, out_Array as array of DicElem, Option_ as Empty )
【引数】
Dic
ソートするデータが入った辞書。(辞書はソートされません)
辞書に入っているデータをシェーカーソートします。 辞書のキーでソートします。
ソートした結果の配列。配列の要素は
out_Array
(src)
Option_
Empty を指定してください
サンプル
  Set dic = CreateObject( "Scripting.Dictionary" )

  ShakerSort_fromDicKey  dic, items, Empty  '//[out] items

  For Each  item  In items
    echo  item.Key +" : "+ item.Item
  Next
dic, items
並べ替え、ソート
Sub  QuickSort( Arr as object array, iLeft as integer, iRight as integer,
                CompareFunc as function, CompareFuncParam as variant )
【引数】
Arr
iLeft
ソートする配列。(*1)
ソートする範囲。配列番号の最小値
配列をクイックソートします。安定ソートではありません。
ソートする範囲。配列番号の最大値
iRight
CompareFunc
CompareFuncParam
CompareFunc の第3引数
Option Explicit

Class  ClassA : Public id : End Class

Redim  arr(5)
Set arr(1) = new ClassA : arr(1).id = 6
Set arr(2) = new ClassA : arr(2).id = 3
Set arr(3) = new ClassA : arr(3).id = 4
Set arr(4) = new ClassA : arr(4).id = 4
Set arr(5) = new ClassA : arr(5).id = 1


QuickSort  arr, 1, UBound( arr ), GetRef("CmpFunc1"), Empty


WScript.Echo  arr(1).id &","& arr(2).id &","& arr(3).id &","& _
              arr(4).id &","& arr(5).id

Function  CmpFunc1( left, right, param )
  CmpFunc1 = left.id - right.id    '// 降順なら right.id - left.id
End Function
サンプル
→ クイックソート
(src)
→ [T_QSort] QuickSort のテスト
配列の要素はオブジェクトであること。
関連
参考
テスト
Arr 引数は、Visual Basic 標準の配列でも、ArrayClass でも構いませんが、
ArrayClass::Items は指定できません。(ソートされません。)
(*1)
Sub  ShakerSort( Arr as array of Object, iLeft as integer, iRight as integer,
                 CompareFunc as function, CompareFuncParam as variant )
【引数】
Arr
iLeft
ソートする配列。(*1)
ソートする範囲。配列番号の最小値
配列をシェーカーソートします。安定ソートです。
ソートする範囲。配列番号の最大値
iRight
CompareFunc
CompareFuncParam
CompareFunc の第3引数
Option Explicit

Class  ClassA : Public id : End Class

Redim  arr(5)
Set arr(1) = new ClassA : arr(1).id = 6
Set arr(2) = new ClassA : arr(2).id = 3
Set arr(3) = new ClassA : arr(3).id = 4
Set arr(4) = new ClassA : arr(4).id = 4
Set arr(5) = new ClassA : arr(5).id = 1


ShakerSort  arr, 1, UBound( arr ), GetRef("CmpFunc1"), Empty


WScript.Echo  arr(1).id &","& arr(2).id &","& arr(3).id &","& _
              arr(4).id &","& arr(5).id

Function  CmpFunc1( Left, Right, Param )
  CmpFunc1 = Left.id - Right.id    '// 降順なら Right.id - Left.id
End Function
サンプル
(src)
→ [T_ShakerSort] ShakerSort のテスト
  Dim    line
  ReDim  lines(-1)
  Set f = OpenForRead( target_result_txt )
  Do Until  f.AtEndOfStream
    Set line = new String1
    line.s = f.ReadLine()
    ReDim Preserve  lines( UBound( lines ) + 1 )
    Set lines( UBound( lines ) ) = line
  Loop
  f = Empty

  ShakerSort  lines, 0, UBound( lines ), GetRef("StrCmpFunc"), 1

Function  StrCmpFunc( Left, Right, Param )
  StrCmpFunc = StrComp( Left.s, Right.s, Param )
End Function

Class  String1
  Public  s
End Class
文字列を昇順にソートするサンプル
配列の要素はオブジェクトであること。
→ PArray_doShakerSort (clib)
テスト
関連
参考
Arr 引数は、Visual Basic 標準の配列でも、ArrayClass でも構いませんが、
ArrayClass::Items は指定できません。(ソートされません。)
(*1)