Hash Table Search

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 6 months ago.

  • Author
    Posts
  • #5362

    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 🙂

    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"
    $hash=@{}
    foreach ($line in $holidays)
    {
    $tempDate,$name = $line -split " "
    $hash.$tempDate = $name
    }
    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"
    }

    if ($counter -eq 50){
    $date50 = $date.AddHours(9) #50th Work Day
    #write-host $date50
    }

    if ($counter -eq 60){
    $date60 = $date.AddHours(9) #60th Work Day
    #write-host $date60
    }

    if ($counter -eq 110){
    $date110 = $date.AddHours(9) #110th Work Day
    #write-host $date110
    }

    if ($counter -eq 120){
    $date120 = $date.AddHours(9) #120th Work Day
    #write-host $date120
    }
    $counter++
    $debug = "$date $counter"
    $debug | out-file C:\debug.txt -Append
    }

    by nohandle at 2012-12-18 10:54:06

    Why is there a negative counter? What are you trying to achieve?
    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.

    by daytime10 at 2012-12-18 10:59:56

    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 😛 but it does seem to work this way too.
    The problem is the hash search is not triggering after the first holiday

    by nohandle at 2012-12-18 11:21:46

    [quote="daytime10"]31.12.2012   Shutdown
    '@ -split "`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 🙂

    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 😛 This can be marked as solved, I just can't because I don't have the button

You must be logged in to reply to this topic.