Page 1 of 1 1
Topic Options
#201243 - 2010-12-21 12:16 PM Scripting Dictionary... Help!
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
I've been playing around with the Scripting Dictionary, and it can be quite usefull. However I can't get a certain command to work in Kix.
To illustrate, this is how the dictionary basically works:
 Code:
$objDict = CreateObject("Scripting.Dictionary")
$objDict.Add("item1","value1")
$objDict.Add("item2","value2")
$objDict.Add("item3","value3")

? "Amount of Items: " + $objDict.Count
? "Value of Item1: " + $objDict("item1")

;Enumeration of Items and Value's
For Each $objItem in $objDict
  ? "Item: " + $objItem " contains value: " + $objDict($objItem)
Next


Pretty simple right?
Now, in VBS you can replace the values by doing:
 Code:
$objDict("item2") = "value ANYTHING"


Do that in Kix, and it will error out.
Since This is COM, I also tried:
 Code:
$objDict("item2").Value = "value ANYTHING"


But that just doesn't change the value, I'm basically at a loss here, hoping anyone here has some insights.

Top
#201244 - 2010-12-21 01:11 PM Re: Scripting Dictionary... Help! [Re: Arend_]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Found an old post about this. Not promising anything good.
Top
#201245 - 2010-12-21 02:13 PM Re: Scripting Dictionary... Help! [Re: Arend_]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Simple answer is use .Remove and then .Add when you want to replace the value.

Use .Exists if you want to do it conditionally.

Wrap it up in a couple of UDFs and you're done.

Top
#201246 - 2010-12-21 04:09 PM Re: Scripting Dictionary... Help! [Re: Richard H.]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Richard, yes that's the "patch" solution, which breaks your "sorted" Items.
It still doesn't solve the original problem.

Top
#201247 - 2010-12-21 08:43 PM Re: Scripting Dictionary... Help! [Re: Arend_]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
What happens when your re .add item2? I'm guessing an error, but worth a shot.
Top
#201248 - 2010-12-21 09:14 PM Re: Scripting Dictionary... Help! [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
Got interested... re.add doesn't work. I saw what you meant about .remove and .add changing the sort order. By the way... a few mods to make your code work right...

 Code:
$objDict = CreateObject("Scripting.Dictionary")
$objDict.Add("item1","value1")
$objDict.Add("item2","value2")
$objDict.Add("item3","value3")

? "Amount of Items: " + $objDict.Count
? "Value of Item1: " + $objDict.item("item1")

;Enumeration of Items and Value's
For Each $objItem in $objDict
  ? "Item: " + $objItem " contains value: " + $objDict.item($objItem)
Next


Powershell can make quick work of this (Sorting, Adding, Removing, Querying)... http://technet.microsoft.com/en-us/library/ee692803.aspx

I'm sure we could make this work with the com object...

Top
#201249 - 2010-12-21 10:52 PM Re: Scripting Dictionary... Help! [Re: Allen]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Your mods are weird that they work, the way I do it is the way it was intended \:\)
PowerShell uses HashTables, which is a total different approach, although I used to LOVE HashTables in MSL \:\)

Top
#201250 - 2010-12-21 11:02 PM Re: Scripting Dictionary... Help! [Re: Arend_]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
Really??? I got errors without making the changes.

 Quote:
ERROR : IDispatch pointers not allowed in expressions!


Edited by Allen (2010-12-22 03:35 AM)

Top
#201251 - 2010-12-22 06:49 AM Re: Scripting Dictionary... Help! [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
WHOHAHAHA.... I hope this is useful (You have no idea how long this took to figure out... but it was fun.)

$objDict = CreateObject("Scripting.Dictionary")
DictionarySetValue($objDict,"item1","value1")
DictionarySetValue($objDict,"item2","value2")
DictionarySetValue($objDict,"item3","value3")
 
? "Amount of Items: " + $objDict.Count ? "Value of Item1: " + $objDict.item("item1")
;Enumeration of Items and Value's For Each $objItem in $objDict ? "Item: " + $objItem " contains value: " + $objDict.item($objItem) Next
DictionarySetValue($objDict,"item2","value ANYTHING") ? "Value of Item2: " + $objDict.item("item2")
;Enumeration of Items and Value's For Each $objItem in $objDict ? "Item: " + $objItem " contains value: " + $objDict.item($objItem) Next


function DictionarySetValue($DictionaryObject, $key, $value)
  dim $sc
  if vartypename($DictionaryObject) <> "Object"
    exit 13
  endif
  if vartypename($key) = "Empty"
    exit 13
  endif
  if $DictionaryObject.Exists($key)
    $sc = CreateObject("ScriptControl")
    $sc.language = "VBScript"
    $sc.addobject('dict',$DictionaryObject)
    $sc.addcode('dict.Item("' + $key + '")="' + $value + '"')
    $sc.run
    $DictionaryObject=$sc.codeobject.dict		
  else
    $DictionaryObject.Add($key, $value)
  endif
endfunction 

Top
#201253 - 2010-12-22 08:35 AM Re: Scripting Dictionary... Help! [Re: Allen]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
LOL nice \:D
Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 718 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.064 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org