Comparison Scripting

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Comparison Scripting

Post by rasimmer »

This is making my brain hurt, so maybe someone else has done this and can help me along. Basically I have 2, one dimensional arrays that are sorted alphabetically. The arrays contain print driver names from servers, which I want to compare what drivers are loaded on serverA and serverB. The output will be in a table in an HTA and basically want it to look something like

SERVERA SERVERB
HP LaserJet 100 --- ' printer only exists on Server B
--- HP LaserJet 150 ' printer only exists on Server A
HP LaserJet 250 HP LaserJet 250 ' printer exists on both servers

I've found plenty of scripts (loops) that find a match, but nothing like what I'm trying to do above. I saw another post from RMueller to use Scripting.Dictionary, but I've never used it for anything. So, any ideas or assistance would be appreciated
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Comparison Scripting

Post by rasimmer »

This is making my brain hurt, so maybe someone else has done this and can help me along. Basically I have 2, one dimensional arrays that are sorted alphabetically. The arrays contain print driver names from servers, which I want to compare what drivers are loaded on serverA and serverB. The output will be in a table in an HTA and basically want it to look something like

SERVERA SERVERB
HP LaserJet 100 --- ' printer only exists on Server B
--- HP LaserJet 150 ' printer only exists on Server A
HP LaserJet 250 HP LaserJet 250 ' printer exists on both servers

I've found plenty of scripts (loops) that find a match, but nothing like what I'm trying to do above. I saw another post from RMueller to use Scripting.Dictionary, but I've never used it for anything. So, any ideas or assistance would be appreciated
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Comparison Scripting

Post by jvierra »

Try doing it like this:







ServerA
ServerB

Driver 1



Driver 2



Driver 3



Driver 4



Driver 5


jvierra2009-02-18 10:13:35
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Comparison Scripting

Post by jvierra »

Please look at my code as it demos the sorting.
The dictioanry is always sorted on teh key.
for each k in dct
WScript.Echo k
next

should print a sorted list of keys.

NO there is no sort method.
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Comparison Scripting

Post by rasimmer »

In your first FOR you add all of the dictionary objects for Server1 and then either add the printer if the key doesn't exist from Server1:
dct.Add element, "Server2"

or append the second second server to the key:

dct(element) = dct(element) & ",Server2"
So, any printers that were added would be on the bottom of the key list. I'm just wondering if there is a 'not complicated' way of sorting them alphabetically. rasimmer2009-02-18 13:18:18
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Comparison Scripting

Post by jvierra »

Sorry - I am thinking of another area of dictionaries not in VBscript.

You are correct. Dictionaries are not sorted which has alwayts been a problem with VBScript.

use the following method.

keys = Sort(dct.Keys)

for each k in keys
WScript.Echo k, dct(k)
next

This will give you a sorted result.

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Comparison Scripting

Post by jvierra »

Here is a complete sort solution.

Code: Select all

	a = Array("J","I","H","G","F","E","D","C","B","A")Set dct = CreateObject("Scripting.Dictionary")For Each x In a   ' WScript.Echo x    dct.add x,0Next
	 
	For Each k In sort(dct.keys,  dct.Count)    WScript.Echo kNext
	 
	Function Sort(keys, cnt)
	    Dim oArrayList,  i    Set oArrayList = CreateObject("System.Collections.ArrayList" )        For i = 0 To cnt - 1        oArrayList.Add keys(i)    Next
	    oArrayList.Sort    set Sort = oArrayList
	 
	End Function
	[code]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Comparison Scripting

Post by jvierra »

And a yet again easier way to derive a sorted list.

We will use a - of all things - "SortedList" object. This object maintains a "Dictionary" like structure that is always sorted. I believe we can alter the sort method and even reverse the sort.

Code: Select all

	
a = Array("J","I","H","G","F","E","D","C","B","A")
Set sortedList = CreateObject("System.Collections.SortedList" )
	
' add the array contents
For Each x In a
    sortedList.Add x, 0
Next
	
 
	
' now print the array and it will be in order.
	
For i = 0 To sortedList.count
    WScript.Echo sortedList.GetKey(i)
Next

This will work on any system with NET Framework 1.1 or later installed.

Too often we forget that the NET framework was opened to COM at version 1.1 and MSCORLIB is automatically installed and registered. MSCORLIB contains numerous classes and collecion classes accessible from script languages.


jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Comparison Scripting

Post by jvierra »

That's the deal. VERY COOL.

Post teh HTML and I will show you a very quick and easy way to generate the table without using string catenation.

What did you end up using for the sort?

User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Comparison Scripting

Post by rasimmer »

Have you ever implemented the 'onclick' attribute using the dynamic table generation. I have tried several things:
img.onclick = vbScript:Test()
img.onclick = "vbScript:Test()"

img.onclick = Test()

img.onclick = "Test()"

img.onclick = Test

img.onclick = "Test"
This topic is 14 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked