Hash Table Search

by daytime10 at 2012-12-18 07:07:42

Having some issues with a hash table search, I basically have a hash table of holidays. It will find the first holiday (Christmas) but any holiday after that it does not trigger. Its suppose to minus the counter 1 if it finds a saturday or sunday or a holiday. I also have a debug setup that will display a message if it finds a holiday but this does not trigger after Christmas… can anyone see any issues?

#Date Calculations
#Company Holidays List
$holidays = @{"25.12.2012" = "Christmas Day"; "26.12.2012" = "Boxing Day"; "27.12.2012" = "Company Holiday"; "28.12.2012" = "Company Holiday"; "31.12.2012" = "Shutdown"}

function format-date ([datetime]$date)
{
"{0:dd.MM.yyyy}" -f $date
}

[datetime]$date = $searchdate
#$date= "11.1.2012"
$counter = 0
while ($counter -le 120) #Count 120 Days checking for holidays/weekends
{
$date = $date.AddDays(1)
if (($date.DayOfWeek -eq [dayofWeek]::Saturday) -or ($date.DayOfWeek -eq [dayofWeek]::Sunday) -or ($hash.ContainsKey((format-date $date))))
{
$counter = $counter - 1
}
if ($hash.ContainsKey((format-date $date))){
Write-Host "Holiday Found $date"
}
by ArtB0514 at 2012-12-18 08:09:31
Yes. The name of your hash is $holidays, not $hash. Also, you only decrement $counter so it will always have a negative value so the search will never end.
#Date Calculations
#Company Holidays List
$holidays = @{"25.12.2012" = "Christmas Day"; "26.12.2012" = "Boxing Day"; "27.12.2012" = "Company Holiday"; "28.12.2012" = "Company Holiday"; "31.12.2012" = "Shutdown"}
function format-date ([datetime]$date){"{0:dd.MM.yyyy}" -f $date}
[datetime]$date = $searchdate
#$date= "11.1.2012"
$counter = 0
while ($counter -le 120) { #Count 120 Days checking for holidays/weekends
$date = $date.AddDays(1)
$fDate = Format-Date $date
if (($date.DayOfWeek -eq [dayofWeek]::Saturday) -or ($date.DayOfWeek -eq [dayofWeek]::Sunday) -or ($holidays.ContainsKey($fDate))) {$counter++}
if ($Holidays.ContainsKey($fDate)){ Write-Host "Holiday Found $fDate"}
}
by nohandle at 2012-12-18 09:41:20
I assume this is followup on this viewtopic.php?f=2&t=669&p=2655&hilit=holiday#p2665
What are you trying to achieve? I had some trouble working with the dates while creating the function. Since I’ve learned that saving the dates in utc fileformat is probably better idea because you don’t have to deal with the formatting issues.
by daytime10 at 2012-12-18 10:38:31
Hey Nohandle, thanks for your help in the past and yes its a followup :slight_smile:

So this is the code I am currently using, the previously pasted code was a test I was running. The problem is that it will find the first holiday without issues, but any other holiday after that it does not seem to trigger the negative counter

#Setup search date format
$fulldate = Get-Date
$day = $fulldate.day
$month = $fulldate.month
$year = $fulldate.year
#$searchdate = "$month/$day/$year 00:00:00"
$searchdate = "12/17/2012 00:00:00"

#Date Calculations
#Company Holidays List
$holidays = @'
25.12.2012 Christmas Day
26.12.2012 Boxing Day
27.12.2012 Company Holiday
28.12.2012 Company Holiday
31.12.2012 Shutdown
'@ -split "r&quot;<br>$hash=@{}<br>foreach &#40;$line in $holidays&#41; <br>{<br> $tempDate,$name = $line -split &quot; &quot; <br> $hash.$tempDate = $name<br>}<br>function format-date &#40;[datetime]$date&#41;<br>{<br> &quot;{0:dd.MM.yyyy}&quot; -f $date<br>}<br><br>[datetime]$date = $searchdate<br>#$date= &quot;11.1.2012&quot;<br>$counter = 0<br>while &#40;$counter -le 120&#41; #Count 120 Days checking for holidays/weekends<br>{<br> $date = $date.AddDays&#40;1&#41;<br> if &#40;&#40;$date.DayOfWeek -eq [dayofWeek]::Saturday&#41; -or &#40;$date.DayOfWeek -eq [dayofWeek]::Sunday&#41; -or &#40;$hash.ContainsKey&#40;&#40;format-date $date&#41;&#41;&#41;&#41; <br> {<br> $counter = $counter - 1<br> }<br> if &#40;$hash.ContainsKey&#40;&#40;format-date $date&#41;&#41;&#41;{<br> Write-Host &quot;Holiday Found $date&quot;<br> }<br> <br> <br> <br> <br> if &#40;$counter -eq 50&#41;{<br> $date50 = $date.AddHours&#40;9&#41; #50th Work Day<br> #write-host $date50<br> }<br> <br> <br> if &#40;$counter -eq 60&#41;{<br> $date60 = $date.AddHours&#40;9&#41; #60th Work Day<br> #write-host $date60<br> }<br><br> <br> if &#40;$counter -eq 110&#41;{<br> $date110 = $date.AddHours&#40;9&#41; #110th Work Day<br> #write-host $date110<br> }<br><br> <br> if &#40;$counter -eq 120&#41;{<br> $date120 = $date.AddHours&#40;9&#41; #120th Work Day<br> #write-host $date120<br> }<br> $counter++<br> $debug = &quot;$date $counter&quot;<br> $debug | out-file C:\debug.txt -Append<br>}</code></blockquote>by nohandle at 2012-12-18 10:54:06<blockquote>Why is there a negative counter? What are you trying to achieve?<br>The idea of the original function was pretty simple. Go to the next day and if the day is not weekend or holiday (ie not in the holiday list) add one to day counter. Repeat. If the day counter reaches one of the values in the day count list (50,60,100,110) the date is output.</blockquote>by daytime10 at 2012-12-18 10:59:56<blockquote>Same sort of idea I am just adding a day every time to the counter but if it detects a holiday or weekend it actually minuses that day. I don't remember why I modified it from how you originally wrote it :P but it does seem to work this way too. <br>The problem is the hash search is not triggering after the first holiday</blockquote>by nohandle at 2012-12-18 11:21:46<blockquote>[quote=&quot;daytime10&quot;]31.12.2012Â Â Shutdown<br>'@ -split &quot;r"[/quote]
You prepare the data incorrectly the original script uses n (new line) to split the input, you use r for some reason. The whole script was just a quick mockup on the issue you could improve this part.

[quote="daytime10"]if it detects a holiday or weekend it actually minuses that day. I don’t remember why I modified it from how you originally wrote it ]
Tested it and it does not work because if you add two holidays (Dec 19 and Dec 20) you get -2 and the next day is working day (friday) you should get Dec 21 as first working day but the function returns Dec 24 instead.
by daytime10 at 2012-12-19 06:34:38
That did it, thanks again for your help :slight_smile:
by nohandle at 2012-12-20 00:16:01
If the issue is resolved please mark the thread as Solved by the solved button.
by daytime10 at 2012-12-20 07:32:36
My Internal web filter \ ad filter must block that solved button because I cannot see it :stuck_out_tongue: This can be marked as solved, I just can’t because I don’t have the button