June 2009 - Posts

Games: Beginner 1

This one is a bit more awkward than the advanced opening event. We need to read a file and print the medal winners of the race. Fairly  standard games question.  Awkward bit is that we get a text file rather than a csv.

 

001
002
003
004
005
006
007
008
009
010
011
012
013
$times = @{}
Get-Content "100 Meter Event.txt." | foreach {
    if (!($_.StartsWith("Name"))) {
        $i = $_.IndexOf(".")
        [double]$time = $_.Substring($i-1)
        $times += @{$_ = $time }     
    }
}
$winners = $times.GetEnumerator() | sort value  | select Name -First 3
$medals = @("Gold", "Silver", "Bronze")
for ($i=0;$i-le 3;$i++){
    "{0,-6} {1}" -f $($medals[$i]), $($Winners[$i].Name)
}

 

The expert commentary on this one uses regular expressions but I don’t like them so back to brute force.

Start with a hash table and read the file.  Loop through the lines in the file – discarding the first line.  Find the decimal point in the time using Indexof() – the lines don’t have a standard format so can’t do splits and get predictable results. garb a substring with the time and use that and the full line in the hash table.

Sort the hash table and pick the three fastest. Define the medals. Loop through the winners and use –f to format the display.  if you want to be fancy we could format the results better but there are no points for style in these games – in fact there are no points at all.

Technorati Tags: ,
Posted by RichardSiddaway | with no comments
Filed under:

Games: Advanced 1

The first task in the advanced section is to  read a text file and find the lines with the shortest length

 

001
002
003
004
005
006
007
008
009
##
## there are empty lines!
$lines = @{}
Get-Content "Personal Information Cards_ADV1.txt" | foreach {
    if ($_.length -gt 1){
        $lines += @{$_ = $_.Length}
    }
}
$lines.GetEnumerator() | sort value | select -First 3

 

Create a hash table. Read the file using get-content.  Pipe into foreach and check the length of the record. Some of the lines are 0 length or have a blank space (length 1) at the beginning. We will assume they should be discarded. The line and its length is then added to the hash table using the line as the key.  If the length is the key errors will be generated because there are multiple lines with the same length.

We can then sort the hash table based on the value (length). Remember we need to use the GetEnumerator() method to be able to sort. We can then select the first three results to give us the shortest lines.

You may find that depending on how you produce your result (other methods are available) may be slightly different.  There are three possible results that could be used for the second and third shortest lines respectively.

Name                           Value
----                           -----
PPID                           4
Street                         6
Gender                         6
Claims                         6
Concepts                       8

Technorati Tags: ,
Posted by RichardSiddaway | with no comments
Filed under:

Scripting Games

The games have begun and the expert commentary on the first event can be found from the script centre.

Looks like the answers get posted before the closing date

Technorati Tags:
Posted by RichardSiddaway | with no comments

Value swapping

One task that seems to crop up fairly frequently is that two variables need to exchange their values. I’ve recently had this issue. I have been looking at a list of mailboxes and one of the questions was when were these mailboxes last accessed. The data is in CSV format so the date information is held as a string - “10/06/2009” – 10th June 2009 in UK format. When try to turn that into a date we can use for comparisons we get

PS> [datetime]"10/6/09"

06 October 2009 00:00:00

Oops.  Not quite what we wanted.  With PowerShell we have to input the string as “mm/dd/yy” ie in US format.  So we need to swap the month and the day around.

In a lot of programming languages we would need to use a third variable as a temporary step

PS> $a=1
PS> $b=2
PS> $a;$b
1
2
PS> $t=$a
PS> $a=$b
PS> $b=$t
PS> $a;$b
2
1

We can make this a good bit shorter

PS> $a=1
PS> $b=2
PS> $a;$b
1
2
PS> $a,$b = $b,$a
PS> $a;$b
2
1

So we make the swap in one line and don’t need the temporary step.

This means I can get my date

PS> $s = "10/06/2009"
PS> $d = $s -split "/"
PS> $d[0],$d[1] = $d[1],$d[0]
PS> [datetime]($d -join "/")

10 June 2009 00:00:00

Plus the –split and –join operators in PowerShell v2 make this easier

Technorati Tags: ,
Posted by RichardSiddaway | with no comments
Filed under:

Bing

Microsoft’s new search engine – Bing – www.bing.com went live recently.

Interestingly it is beginning to show strongly in this blog’s search stats.

Technorati Tags:
Posted by Richard's space
Filed under: ,

PowerGUI 1.8

PowerGUI 1.8 is now available for download from www.powergui.org

The attendees at the last PowerShell User Group meeting will remember Dmitry’s demonstration of the new features in 1.8

It does run on Windows 7 RC

Enjoy.

Technorati Tags: ,
Posted by Richard's space
Filed under: ,
More Posts « Previous page