Powershell Scripting Games - Day 1

If you have not heard about it yet - the Summer Scripting Games 2009 have started.

Well I am one day late (sorry!!) but I was trying to figure out the solution for the Event for Day 1 -Beginner - it was late last night and my brain was not working too well.

So I looked online today and want to thank Hugo Peters for helping me out with his solution for this event.

The problems I had with this solution was the fact that the file was not formatted correctly - there were spaces and tabs, therefore I had to parse the file and replace the spaces with tabs(Line 7).
Also how to extract the information and to place it into an array (Lines 13 - 24).

Here you go.

# Read the input file
$InputFile = ".\100 Meter Event.txt"
$Content = Get-Content $InputFile
  
#Since the lines are not all separated with tabs and some are with spaces,
#we will replace all spaces in the file with tabs
$Content = $Content | foreach-object { $_ -replace "(.*)\s(.*)","`$1`t`$2" } 
  
# Prepare output collection
$myCollection = @()
  
# Loop through lines in the file, exept the first one
ForEach ($Line in ($Content[1..$($Content.Length)]))
     {
     # Properties are separated by tabs
     $myObj = "" | Select Name, Country, Time #Create a new object with the columns
     #Splitting the line on the tab returns 3 values - we get each one in turn and 
     #add it to the appropriate column
     $myObj.Name = $Line.Split("`t")[0] #Retrieve the Name
     $myObj.Country = $Line.Split("`t")[1] #Retrieve the Country
     $myObj.Time = $Line.Split("`t")[2] #Retrieve the time
     # Add to output
     $myCollection += $myObj
     }
# Now we sort the the object on the Time column and Select the top 3 results
$myCollection | Sort Time | Select -First 3