Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Comparing 2 dictionary objects

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,38688
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Comparing 2 dictionary objects
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Comparing 2 dictionary objects - 10/13/2006 12:35:40 AM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi all,

I have a wsh script which builds to different dictionary objects. One is a hash containing files and their corresponding thresholds. The other hash gets the actual files and actual sizes of the files. Hence, I need to compare the the values of the actual file sizes to those of the threshold and report the ones which are above the threshold. Now I have tried using the EXISTS method on 'the second object' to get the file size values (file names are the key and are the same for both dictionary objs) to no avail. Is there a way of doing this?

Any help will greatly be appreciated!

Thanks  in advance,
Chucksk  
 
 
Post #: 1
 
 RE: Comparing 2 dictionary objects - 10/13/2006 3:39:02 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
Please post actual (or dummy) samples of the contents of each dictionary.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 2
 
 RE: Comparing 2 dictionary objects - 10/15/2006 7:37:35 PM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi there,
Below is the snippet of code that i think is not working (in bold). As you can see i am trying to fetch the the values from another dictionary object using the same key which exists for both dictionary objects. Looping through just the dctThresholds dictionary works fine. The problem arises when i loop through one dictonary and fetch the values of another as well. I wouldnt have thought this would be a problem as the keys are the same? Any help would be greatly apprecaited.

actualcolkeys = dctRealVals.Keys
For Each actualstrTbsName in actualcolkeys
actualstrTbs = trim(actualstrTbsName)
WScript.Echo actualstrTbs
WScript.Echo "dctRealVals.Exists(" & actualstrTbs & ")"
WScript.Echo dctRealVals.Exists(actualstrTbs)
If dctRealVals.Exists(actualstrTbs) Then
 defaultcolvals= dctThresholds.Item(actualstrTbs)
 actualcolvals = dctRealVals.Item(actualstrTbs)
 WScript.Echo "dctThresholds.Item(" & actualstrTbs & ")"
 WScript.Echo "The threshold is" & defaultcolvals
 WScript.Echo actualstrTbs & " " & defaultcolvals & " " & actualcolvals
End If
Next


(in reply to chucksk)
 
 
Post #: 3
 
 RE: Comparing 2 dictionary objects - 10/15/2006 7:41:37 PM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
This is a sample thresholds file (the ',' are splitted out):

DRSYS                         ,       25                                      
EXAMPLE                       ,       25                                       
SYSTEM                        ,       25                                       
TEMP                          ,       25                                       
TOOLS                         ,       25                                       
UNDOTBS1                      ,       25                                       
USERS                         ,       25                                       

This is the output generated by the query ( the output is pumped into an array via standard out):                                          

                                  
DRSYS                    24
EXAMPLE                  4
SYSTEM                   76
TEMP                     8
TOOLS                    100
UNDOTBS1                 3
USERS                    0

The only thing i thought which would not match would be the spaces, however i do a trim on all objects before putting them into the dictionary!

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: Comparing 2 dictionary objects - 10/16/2006 1:32:06 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
First, I would be suprised if this:

If dctRealVals.Exists(actualstrTbs)

ever evaluates to true, because you do this:

actualstrTbs = trim(actualstrTbsName)

So essentially if the key exists in the dictionary with white space, you are looking for it to exist in the dictionary without white space.

If you think the keys are the same and the script does not, the script is never wrong. Try this code and verify that the keys really are the same:

For Each strKey In dctRealVals.Keys()
WScript.Echo "|" & Trim(strKey) & "|"
Next
For Each strKey In dctRealVals.Keys()
WScript.Echo "|" & strKey & "|"
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 5
 
 RE: Comparing 2 dictionary objects - 10/16/2006 7:45:30 PM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi ebgreen,

This is the output i get after running your code:

|DRSYS                  |
|EXAMPLE                |
|SYSTEM                 |
|TEMP                   |
|TOOLS                  |
|UNDOTBS1               |

|USERS                  |
|DRSYS                  |
|EXAMPLE                |
|SYSTEM                 |
|TEMP                   |
|TOOLS                  |
|UNDOTBS1               |
|USERS                  |

So it seems to me that the trim is not working? Am getting more confused about this, sorry am new to VBScript! Is there a way to compare them then?

Thanks in advance

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: Comparing 2 dictionary objects - 10/16/2006 11:20:36 PM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
It does indeed appear that the trim is not working. Lets see what those strings are really made of. Run this code to see what characters are really comprising the strings:

For Each strKey In dctRealVals.Keys()
WScript.Echo "|" & Trim(strKey) & "|"
For i = 1 To Len(strKey)
   WScript.Echo vbTab & Asc((Mid(strKey, i, 1))
Next
Next
For Each strKey In dctRealVals.Keys()
WScript.Echo "|" & strKey & "|"
For i = 1 To Len(strKey)
    WScript.Echo vbTab & Asc((Mid(strKey, i, 1))
  Next
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 7
 
 RE: Comparing 2 dictionary objects - 10/16/2006 11:46:21 PM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi there,

Got some interesting results:

|DRSYS                  |
       68
       82
       83
       89
       83
       9
       9
       9
|EXAMPLE                |
       69
       88
       65
       77
       80
       76
       69
       32
       9
       9
|SYSTEM                 |
       83
       89
       83
       84
       69
       77
       9
       9
       9
|TEMP                   |
       84
       69
       77
       80
       9
       9
       9
|TOOLS                  |
       84
       79
       79
       76
       83
       9
       9
       9
|UNDOTBS1               |
       85
       78
       68
       79
       84
       66
       83
       49
       9
       9
|USERS                  |
       85
       83
       69
       82
       83
       9
       9
       9
|DRSYS                  |
       68
       82
       83
       89
       83
       9
       9
       9
|EXAMPLE                |
       69
       88
       65
       77
       80
       76
       69
       32
       9
       9
|SYSTEM                 |
       83
       89
       83
       84
       69
       77
       9
       9
       9
|TEMP                   |
       84
       69
       77
       80
       9
       9
       9
|TOOLS                  |
       84
       79
       79
       76
       83
       9
       9
       9
|UNDOTBS1               |
       85
       78
       68
       79
       84
       66
       83
       49
       9
       9
|USERS                  |
       85
       83
       69
       82
       83
       9
       9
       9

Whats does this mean?..Thanks again for all yer help!

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: Comparing 2 dictionary objects - 10/17/2006 12:08:57 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
It means that the white space is tabs. Trim does not remove tabs, but we can remove them explicitly. Instead of using Trim to remove the whitespace, use Replace.

actualstrTbs = Replace(actualstrTbsName, vbTab, "")

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 9
 
 RE: Comparing 2 dictionary objects - 10/17/2006 1:15:29 AM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi there,

Am getting there now, however now the code fails to resolve the value after i pass the key:

actualcolkeys = dctRealVals.Keys
For Each actualstrTbsName in actualcolkeys
actualstrTbs = Replace(actualstrTbsName, vbTab, "")
actualstrTbs = Trim(actualstrTbs)
'WScript.Echo "|" & actualstrTbs & "|"
'WScript.Echo "dctRealVals.Exists(" & actualstrTbs & ")"
WScript.Echo dctRealVals.Exists(actualstrTbs)
If dctThresholds.Exists(actualstrTbs) Then
 WScript.Echo "Do i get here?"
 defaultcolvals = dctThresholds.Item(actualstrTbs)
 actualcolvals = dctRealVals.Item(actualstrTbs)
 WScript.Echo "dctRealVals.Item(" & actualstrTbs & ")"
 WScript.Echo "dctThresholds.Item(" & actualstrTbs & ")"
 WScript.Echo "The threshold is " & defaultcolvals
 WScript.Echo "The real value is " & actualcolvals
 WScript.Echo actualstrTbs & " " & defaultcolvals & " " & actualcolvals
End If
Next

Sample output:

Do i get here?
dctRealVals.Item(DRSYS)
dctThresholds.Item(DRSYS)
The threshold is25
The real value is
DRSYS 25
0

So the real value is missing? Now is that because of tabs as well? Cause i did this when i am actually entering the key/values into the dict obj:

Do Until objExec.StdOut.atEndOfStream
   strNextLine = objExec.StdOut.ReadLine
   arrValueList = Split(strNextLine , ",")
   For i = 1 to Ubound(arrValueList)
    'WScript.Echo Ubound(arrValueList)
       dctRealVals.Add Replace(arrValueList(0), vbTab, ""), Replace(arrValueList(i), vbTab, "")
    'WScript.Echo "Adding" & trim(arrValueList(0)) & "," & arrValueList(i)
   Next 
Loop


Is this the wrong place to do it?

Thanks in advance

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Comparing 2 dictionary objects - 10/17/2006 1:21:50 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
I thnk we need to regroup. I'm having a hard time keeping the goal in sight. Please tell us exactly what you want the outcome to be and what the input to the script is.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 11
 
 RE: Comparing 2 dictionary objects - 10/17/2006 1:35:58 AM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi,

Sorry to confuse u!

Have a  set of tablespace names and values which are thresholds. These are in a text file:

DRSYS                         ,       25                                      
EXAMPLE                       ,       25                                       
SYSTEM                        ,       25                                       
TEMP                          ,       25                                       
TOOLS                         ,       25                                       
UNDOTBS1                      ,       25                                       
USERS                         ,       25                                       

Then i run a sql query to get the output from a db giving me the tablepsace name and the % used for each:

DRSYS              24
EXAMPLE                4
SYSTEM              76
TEMP               8
TOOLS             100
UNDOTBS1               3
USERS               0

The set of thresholds and actual results are stored in 2 separate dictionary objects. Now i use the tablespace name as the key and the idea is that because the keys are the same on each dictionary, i can obtain the values thus enabling me to check if the pct used is over the threshold! Now we verified that the output from the sql had tabs in it and thats why it was not recognising the keys. After using the Replace function i can see that it is actually chopping the tabs of and getting the the thesold from the thresholds dict. However it is not getting the actual result so i have nothing to compare!

Here is the code in full:

If WScript.Arguments.Count = 0 Then
WScript.Echo "Oracle Username and Password not specified"
WScript.Echo "Usage: cscript scriptname SID username password"
errLevel = 1
WScript.Quit(errLevel)
End If
strSID = Wscript.Arguments.Item(0)
strUsername = Wscript.Arguments.Item(1)
strPassword = Wscript.Arguments.Item(2)
strTbsThresholds = Wscript.Arguments.Item(3)
Const ForReading = 1

strOSCommandText = "cmd /c sqlplus -s " &  strUsername & "/" & strPassword & "@" & strSID & " @sqltest"
WScript.Echo strOSCommandText
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set dctThresholds = CreateObject("Scripting.Dictionary")
dctThresholds.CompareMode = TextMode
Set dctRealVals = CreateObject("Scripting.Dictionary")
dctRealVals.CompareMode = TextMode
'Retrieve the default thresholds and put them in a hash array
Set objTbsThresholds = objFSO.OpenTextFile(strTbsThresholds, ForReading)
Do While objTbsThresholds.AtEndofStream <> True
strText = objTbsThresholds.ReadLine
'WScript.Echo strText
'Split the string and put contents in an array
strTextArray = Split(strText, ",")
 'Add the key/value pairs into a hash array
 For x = 1 to Ubound(strTextArray)
  dctThresholds.Add Trim(strTextArray(0)), Trim(strTextArray(x))
 Next
Loop
Set objExec = WshShell.Exec(strOSCommandText)

' Retrieve the actual tablespace usage and put it into another hash array
Do Until objExec.StdOut.atEndOfStream
   strNextLine = objExec.StdOut.ReadLine
   arrValueList = Split(strNextLine , ",")
   For i = 1 to Ubound(arrValueList)
    'WScript.Echo Ubound(arrValueList)
       dctRealVals.Add Replace(arrValueList(0), vbTab, ""), Replace(arrValueList(i), vbTab, "")
    'WScript.Echo "Adding" & trim(arrValueList(0)) & "," & arrValueList(i)
   Next 
Loop
'Loop through the default key/value pairs
defaultcolkeys = dctThresholds.Keys
For Each strTbsName in defaultcolKeys
   defaultcolvals = dctThresholds.Item(strTbsName)
   lenstrTbsName = len(strTbsName)
   Wscript.Echo "|" & strTbsName & "|" & defaultcolvals & " " & lenstrTbsName
Next
'Loop through the actual results
actualcolkeys = dctRealVals.Keys
For Each actualstrTbsName in actualcolkeys
       actualcolvals = dctRealVals.Item(actualstrTbsName)
WScript.Echo actualstrTbsName & " " & actualcolvals
Next

actualcolkeys = dctRealVals.Keys
For Each actualstrTbsName in actualcolkeys
actualstrTbs = Replace(actualstrTbsName, vbTab, "")
actualstrTbs = Trim(actualstrTbs)
WScript.Echo dctRealVals.Exists(actualstrTbs)
If dctThresholds.Exists(actualstrTbs) Then
 WScript.Echo "Do i get here?"
 defaultcolvals = dctThresholds.Item(actualstrTbs)
 actualcolvals = dctRealVals.Item(actualstrTbs)
 WScript.Echo "dctRealVals.Item(" & actualstrTbs & ")"
 WScript.Echo "dctThresholds.Item(" & actualstrTbs & ")"
 WScript.Echo "The threshold is " & defaultcolvals
 WScript.Echo "The real value is " & actualcolvals
 WScript.Echo actualstrTbs & " " & defaultcolvals & " " & actualcolvals
End If
Next
WScript.Quit(errLevel)

I hope this makes it a tad clear. Let me know if it does not!

Thanks again

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Comparing 2 dictionary objects - 10/17/2006 5:14:12 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
First I would lose the .CompareMode = TextMode commands. 

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 13
 
 RE: Comparing 2 dictionary objects - 10/17/2006 7:59:01 PM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Hi there,

I have taken out the comparemode method. I have also made some progress. I can now pull the tbs name the pct used and the threshold for that particular threshold! However the problem i have now is that i cannot compare the actual pct used with the threshold (i.e <,>). I think this due to the fact that the actual results have some 'invisible' characters in there as well. I edited the last snip of code so that it looks this now:

actualcolkeys = dctRealVals.Keys
For Each actualstrTbsName in actualcolkeys
actualstrTbs = Replace(actualstrTbsName, vbTab, "")
actualstrTbs = Trim(actualstrTbs)
WScript.Echo dctRealVals.Exists(actualstrTbs)
actualcol = dctRealVals.Item(actualstrTbsName)
actualcolt = Replace(actualcol, vbTab, "")
WScript.Echo "|" & actualcolt & "|"
 For i = 1 To Len(actualcolt)
    WScript.Echo vbTab & Asc((Mid(actualcolt, i, 1)))
 Next
If dctThresholds.Exists(actualstrTbs) Then
 defaultcolvals = dctThresholds.Item(actualstrTbs)
 WScript.Echo "The tbs is " & actualstrTbs & "The pct used is " & actualcolt & "The threshold is " & defaultcolvals
 If CInt(actualcolt0) >= CInt(defaultcolvals) Then
  WScript.Echo "I got here"
 End If
End If
Next

Notice i have a replace function in there to replace the tabs (as per your suggestion last time around). However it does not seem to make any difference:

The tbs is TOOLS The pct used is       100The threshold is 25
0
|        3|
       32
       32
       32
       32
       32
       32
       32
       32
       51

My queston is: Are those tabs in there or something else? As u can see i can now get the tablespace name, the pct used and its threshold. All I need is the compare to work!

Thanks in advance,
Chucksk

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: Comparing 2 dictionary objects - 10/18/2006 12:57:06 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
ASCII value 32 is a space, so using Trim on the variable should take care of the problem.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chucksk)
 
 
Post #: 15
 
 RE: Comparing 2 dictionary objects - 10/18/2006 1:18:45 AM   
  chucksk

 

Posts: 13
Score: 0
Joined: 10/13/2006
Status: offline
Thanks Ebgreen. That has sorted the problem and the script now works.

Thanks again for all your input.

(in reply to ebgreen)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Comparing 2 dictionary objects Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts