« Microsoft Scripting Olympics | Main | Event 2: Skating on Thin Ice »

Event 1: Could I get your Phone Number?

Here is my submission for the Advanced Event 1 in the Microsoft Scripting Games 2008

   1: '*********************************************************************

   2: ' Script Name: Event1.vbs

   3: '     Version: 1.0

   4: '      Author: Perry Harris (PHactotum)

   5: '     Updated: 12:04 AM Wednesday, February 20, 2008

   6: '     Purpose: Solves the 2008 winter Scripting Games Advance Event 1: Could I get your Phone Number?

   7: '              

   8: '       Usage: cscript Event1.vbs [phone number]

   9: '       Notes:

  10: '    Keywords: 

  11: '  versioning: 1.0    Original release

  12: '*********************************************************************

  13: Option Explicit

  14: Dim strNumber

  15: Dim colMatches

  16: Dim objRegEx

  17: Dim Nums

  18: Dim objFS

  19: Dim objFile

  20: Dim strAll

  21: Dim i

  22: Const ForReading = 1

  23:  

  24: ' Retreive our phone number

  25: if WScript.Arguments.Count < 1 then

  26:     WScript.StdOut.Write "Please enter a 7 digit phone number: "

  27:     strNumber = WScript.StdIn.ReadLine

  28: else

  29:     strNumber = WScript.Arguments(0)

  30: end if

  31:  

  32: 'Verify our phone number is 7 digits and only uses the digits 2 - 9

  33: Set objRegEx = CreateObject("VBScript.RegExp")

  34: objRegEx.Pattern = "[2-9]{7}"

  35: Set colMatches = objRegEx.Execute(strNumber)

  36: if colMatches.Count <> 1 then

  37:     WScript.StdOut.WriteLine "I'm sorry, we only accept 7 digit numbers using the numbers 2 - 9."

  38:     WScript.StdOut.WriteLine "Please try again."

  39:     WScript.Quit

  40: end if

  41:  

  42: 'Convert our phone number to a regular expression

  43: set Nums=CreateObject("Scripting.Dictionary")

  44: Nums.Add 2, "[aAbBcC]"

  45: Nums.Add 3, "[dDeEfF]"

  46: Nums.Add 4, "[gGhHiI]"

  47: Nums.Add 5, "[jJkKlL]"

  48: Nums.Add 6, "[mMnNoO]"

  49: Nums.Add 7, "[pPrRsS]"

  50: Nums.Add 8, "[tTuUvV]"

  51: Nums.Add 9, "[wWxXyY]"

  52:  

  53: for i = 2 to 9

  54:     strNumber = replace(strNumber,i,Nums.item(i))

  55: next

  56:  

  57: objRegEx.Pattern = strNumber & "\r\n"

  58:  

  59: 'Open and read our WordList

  60: Set objFS = CreateObject("Scripting.FileSystemObject")

  61: Set objFile = objFS.OpenTextFile("C:\Scripts\wordlist.txt", ForReading)

  62:  

  63: strAll = objFile.Readall

  64:  

  65: 'Now let's see if there is a match in our word list

  66: Set colMatches = objRegEx.Execute(strAll)  

  67:  

  68: If colMatches.Count > 0 Then

  69:     Wscript.StdOut.WriteLine colMatches(0)

  70: else

  71:     WScript.StdOut.WriteLine "Sorry, no Matches were found"

  72: End If


Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on February 20, 2008 1:45 AM.

The previous post in this blog was Microsoft Scripting Olympics.

The next post in this blog is Event 2: Skating on Thin Ice.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 4.1