I'm going to start by doing a sort for integer arrays with five members. I'll generalize later.
First of all, how do you know whether something is sorted?
That took me an incredibly long time – I assumed I could easily write a script block that would do what I wanted. No – it was not taking an array as its argument. I'm still not sure why.
I finally turned it into a function and specified the parameter in the function signature. This works, but I don't know why the alternative (with the param keyword) does not.
function isSorted ([int32[]]$a) {
for ($i = 0; $i -lt 3; $i++)
{
if ($a[$i] -gt $a[$i+1])
{
return (1 -eq 0)
}
}
return (1 -eq 1)
}
isSorted $sorted
isSorted $unsorted
The result of this is:
True
False
Hang on – I'll try to fix the function to take an array of any length. Here's the key line:
for ($i = 0; $i -lt ($a.length - 2); $i++)
OK, that'll do for today.
Housekeeping note:
I may not be able to keep this blog going 5 days a week, as I have been. I'm starting a new contract on Monday and I won't have quite so much time. I'll try to not let it fall by the wayside.
No comments:
Post a Comment