Bookmark and Share

(Note: These solutions were written for Beginner Event 9 of the 2010 Scripting Games.)

 

Beginner Event 9 (Windows PowerShell)

Daniele Muscetta’s journey with computers and software started when he began programming on a Commodore 64 at the age of 10. Daniele preferred to write programs on his Commodore 64 rather than playing games with it like the other kids did. Today, Daniele works at Microsoft Italy as a premier field engineer who specializes in both System Center Operations Manager and scripting technologies such as VBScript and Windows PowerShell. Daniele maintains a personal blog on his Web site. He has contributed to Scripting Games in the past, and to Ed Wilson’s Windows PowerShell 2.0 Best Practices book.

------------

Windows PowerShell is extremely powerful and easy at the same time in its way of handling arrays. Actually, this solution does not even need a script. It can be done in a one-liner as I'll show later. By now let's start slowly and let's do things one at the time.

Let's initialize the two arrays by providing their names and assigning the values:

$arr1 = "a","b","c","d"

$arr2 = "d","g","f","e"


Now, how do we add them together? In other languages, arrays are less flexible. You can't just put two together, and in many languages, you can't even resize them: have they been initialized with four elements? They'll stay with 4 elements.

In Windows PowerShell, the solution is sweet and simple: We can add or remove elements from an array anytime. In this specific situation, we can just merge the two with the addition operator, the plus sign:

$arr1 + $arr2


By doing this we get a new array that has all of the eight elements combined together. Now it is only a matter of sorting. For this we will use the Sort-Object cmdlet, so let's pass our new joint array to this cmdlet via a pipeline:

$arr1 + $arr2 | Sort-Object


This will sort the elements alphabetically, but won't remove the duplicates. Luckily, Sort-Object provides a switch parameter which lets us remove the duplicates:

$arr1 + $arr2 | Sort-Object -unique


All done!

 

Image of results of running script

 

Now, all the above can even be rewritten without having to assign the arrays to variable names ($arr1 and $arr2) as we did, but we can simply use those sets of values as arrays by simply wrapping them in parentheses. This would make the following one-liner functionally identical to the above: 

("a","b","c","d") +  ("d","g","f","e") | Sort-Object -unique  

 

Image of results of running one-line script

 

 

 

Beginner Event 9 (VBScript)

 

Photo of Salvador Manaois III

 

Salvador Manaois III is a senior systems engineer at Infineon Technologies Asia Pacific Pte Ltd. He currently holds the MCITP certification for both Enterprise Administrator and Server Administrator. He actively evangelizes the use of automation (through scripts and other technologies) both at work and in the various IT user groups in which he is involved. He is also a moderator for The Official Scripting Guys Forum and maintains the Bytes & Badz and Scripting, the SysAdmin Way blogs.

-----------

I have two scripts for this challenge. One uses the Dictionary object class; the other leverages the .NET Framework’s ArrayList class.

I started with declaring the two arrays (arrItems1 and arrItems2) and their contents. Using the split and join functions, I merged the two arrays into the combinedArray variable.

Here is where the difference between the scripts starts. For the BubbleSort.vbs script, I used a simple bubble sort routine to sort the array elements. After they were sorted, I added each unique element into the dictionary object; if the value of an element is the same as one already inside the dictionary object, it will be skipped and will not be added into the dictionary. The combinedArray variable is then re-initialized, emptying its contents. It is then repopulated with values from the dictionary object’s keys and each element displayed in the command-line interface.

BubbleSort.vbs

arrItems1 = Array("a","b","c","d")

arrItems2 = Array("d","g","f","e") 

combinedArray = split(join(arrItems1,"_") & "_" & join(arrItems2,"_") ,"_") 

Set objDictionary = CreateObject("Scripting.Dictionary")

For i = (UBound(combinedArray) - 1) to 0 Step -1

    For j= 0 to i

        If UCase(combinedArray(j)) > UCase(combinedArray(j+1)) Then

            strHolder = combinedArray(j+1)

            combinedArray(j+1) = combinedArray(j)

            combinedArray(j) = strHolder

        End If

    Next

Next 

For Each strItem in combinedArray

    If Not objDictionary.Exists(strItem) Then

        objDictionary.Add strItem, strItem

    End If

Next 

intItems = objDictionary.Count - 1 

ReDim combinedArray(intItems) 

i = 0

For Each strKey in objDictionary.Keys

    combinedArray(i) = strKey

    Wscript.Echo strKey

    i = i + 1

Next


For the Datalist.vbs script, I initially defined the variable DataList of the object class ArrayList. I then added the elements within combinedArray into DataList and used the Sort method to sort them. The elements of DataList are then checked for duplicate or similar entries (starting from the last element), and if one is found, it is removed from DataList. The elements are then displayed in the command-line interface.


Datalist.vbs

arrItems1 = Array("a","b","c","d")

arrItems2 = Array("d","g","f","e") 

combinedArray = split(join(arrItems1,"_") & "_" & join(arrItems2,"_") ,"_") 

Set DataList = CreateObject("System.Collections.ArrayList") 

For i = 0 to UBound(combinedArray)

   DataList.Add combinedArray(i)

Next

pos = 0 

DataList.Sort() 

For i = DataList.Count - 1 To 1 Step -1

  If DataList(i)=DataList(i-1) Then

  DataList.RemoveAt(i)

  End If

Next 

For Each strItem in DataList

    Wscript.Echo strItem

Next


The following image shows the output from running both scripts.

 

Image showing output from running both scripts

 

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
 

Ed Wilson and Craig Liebendorfer, Scripting Guys