1

Тема: AHK: Приближение значения 1 переменной к другой при сравнении.

У меня есть массив:

route := {"20:00:00": "001", "20:30:00": "002", "21:00:00": "003"}

Затем идёт сравнение с массивом:


FormatTime, Var,, HH:mm:ss
if route.HasKey(Var)
	Var2 := route[Var]

Теперь самое главное:
Мне нужно что-бы если например Var = 20:02:00, она определялась как 001, а скажем 20:20:00 как 002.
Т.е как бы приближалась к одному из значений.

p.s: я чайник, объясните поподробнее. Спасибо.

2

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

А если 09:00:00, тогда что должно быть?

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Skype dmitry_fiveg

3

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

teadrinker, я просто сократил массив, на самом деле там от 00:00 до 23:00 время стоит, не буду я же сюда всё копировать.

4 (изменено: teadrinker, 2019-10-20 00:15:52)

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

Попробуйте так:

route := {"20:00:00": "001", "20:30:00": "002", "21:00:00": "003"}
FormatTime, Var,, HHmmss
Var := "20000101" . Var
prevDiff := 0xFFFFFFFF
for k, v in route {
   time := "20000101" . StrReplace(k, ":")
   diff := Var
   diff -= time, s
   if ( Abs(diff) < prevDiff )
      min := v, prevDiff := Abs(diff)
}
MsgBox, % min
Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Skype dmitry_fiveg

5

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

teadrinker, огромное спасибо, работает, НО, как быть, если 23:45, например. Приближает к 23:00, а не 00:00

6

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

Ой, сори, опечатка была, исправил.

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Skype dmitry_fiveg

7

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

Тогда такой костыль можно вставить:

route := {"20:00:00": "001", "20:30:00": "002", "21:00:00": "003", "22:00:00": "004", "23:00:00": "005", "00:00:00": "006"}
Var := "234000"
Var := "20000101" . Var
prevDiff := 0xFFFFFFFF
for k, v in route {
   if SubStr(Var, 9, 2) = "23" && k = "00:00:00"
      k := "23:59:59"
   time := "20000101" . StrReplace(k, ":")
   diff := Var
   diff -= time, s
   if ( Abs(diff) < prevDiff )
      min := v, prevDiff := Abs(diff)
}
MsgBox, % min
Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Skype dmitry_fiveg

8

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

teadrinker, огромнейшее спасибо!

9

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

Или можно дополнительный ключ "23:59:59" вставить с тем же значением, что и "00:00:00".

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Skype dmitry_fiveg

10 (изменено: Dozilius, 2019-10-20 03:01:59)

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

teadrinker. так... Вот в массиве номера расписания и время их начала. А можно как-то сделать "защиту" от выдачи расписания которое уже "началось"?

routes := {"00:00:00": "101", "01:00:00": "103", "02:00:00": "105", "03:00:00": "107", "04:00:00": "109", "05:00:00": "111", "06:00:00": "113", "07:00:00": "115", "07:20:00": "117", "07:40:00": "119", "08:00:00": "121", "08:20:00": "123", "08:40:00": "125", "09:00:00": "127", "09:20:00": "129", "09:40:00": "131", "10:00:00": "133", "10:20:00": "135", "10:40:00": "137", "11:00:00": "139", "11:20:00": "141", "11:40:00": "143", "12:00:00": "001", "12:20:00": "003", "12:40:00": "005", "13:00:00": "007", "13:20:00": "009", "13:40:00": "011", "14:00:00": "013", "14:20:00": "015", "14:40:00": "017", "15:00:00": "019", "15:20:00": "021", "15:40:00": "023", "16:00:00": "025", "16:20:00": "027", "16:40:00": "029", "17:00:00": "031", "17:20:00": "033", "17:40:00": "035", "18:00:00": "037", "18:20:00": "039", "18:40:00": "041", "19:00:00": "043", "19:20:00": "045", "19:40:00": "047", "20:00:00": "145", "20:20:00": "147", "20:40:00": "149", "21:00:00": "151", "21:20:00": "153", "21:40:00": "155", "22:00:00": "157", "23:00:00": "159", "00:30:00": "102", "01:30:00": "104", "02:30:00": "106", "03:30:00": "108", "04:30:00": "110", "05:30:00": "112", "06:30:00": "114", "07:10:00": "116", "07:30:00": "118", "07:50:00": "120", "08:10:00": "122", "08:30:00": "124", "08:50:00": "126", "09:10:00": "128", "09:30:00": "130", "09:50:00": "132", "10:10:00": "134", "10:30:00": "136", "10:50:00": "138", "11:10:00": "140", "11:30:00": "142", "11:50:00": "144", "12:10:00": "002", "12:30:00": "004", "12:50:00": "006", "13:10:00": "008", "13:30:00": "010", "13:50:00": "012", "14:10:00": "014", "14:30:00": "016", "14:50:00": "018", "15:10:00": "020", "15:30:00": "022", "15:50:00": "024", "16:10:00": "026", "16:30:00": "028", "16:50:00": "030", "17:10:00": "032", "17:30:00": "034", "17:50:00": "036", "18:10:00": "038", "18:30:00": "040", "18:50:00": "042", "19:10:00": "044", "19:30:00": "046", "19:50:00": "048", "20:10:00": "146", "20:30:00": "148", "20:50:00": "150", "21:10:00": "152", "21:30:00": "154", "21:50:00": "156", "22:30:00": "158", "23:30:00": "160"}
FormatTime, routetime,, HHmmss
routetime := "20000101" . routetime
prevDiff := 0xFFFFFFFF
for k, v in routes {
   if SubStr(routetime, 9, 2) = "23" && k = "00:00:00"
      k := "23:59:59"
   time := "20000101" . StrReplace(k, ":")
   diff := routetime
   diff -= time, s
   if ( Abs(diff) < prevDiff )
      route := v, prevDiff := Abs(diff)
}

Т.е по факту сделать так, что-бы в 1:02 выдавалось расписание не на 1:00, а на 1:30.

Заранее спасибо!

11

Re: AHK: Приближение значения 1 переменной к другой при сравнении.

Как-то так:

routes := {"00:00:00": "101", "01:00:00": "103", "02:00:00": "105", "03:00:00": "107", "04:00:00": "109", "05:00:00": "111", "06:00:00": "113", "07:00:00": "115", "07:20:00": "117", "07:40:00": "119", "08:00:00": "121", "08:20:00": "123", "08:40:00": "125", "09:00:00": "127", "09:20:00": "129", "09:40:00": "131", "10:00:00": "133", "10:20:00": "135", "10:40:00": "137", "11:00:00": "139", "11:20:00": "141", "11:40:00": "143", "12:00:00": "001", "12:20:00": "003", "12:40:00": "005", "13:00:00": "007", "13:20:00": "009", "13:40:00": "011", "14:00:00": "013", "14:20:00": "015", "14:40:00": "017", "15:00:00": "019", "15:20:00": "021", "15:40:00": "023", "16:00:00": "025", "16:20:00": "027", "16:40:00": "029", "17:00:00": "031", "17:20:00": "033", "17:40:00": "035", "18:00:00": "037", "18:20:00": "039", "18:40:00": "041", "19:00:00": "043", "19:20:00": "045", "19:40:00": "047", "20:00:00": "145", "20:20:00": "147", "20:40:00": "149", "21:00:00": "151", "21:20:00": "153", "21:40:00": "155", "22:00:00": "157", "23:00:00": "159", "00:30:00": "102", "01:30:00": "104", "02:30:00": "106", "03:30:00": "108", "04:30:00": "110", "05:30:00": "112", "06:30:00": "114", "07:10:00": "116", "07:30:00": "118", "07:50:00": "120", "08:10:00": "122", "08:30:00": "124", "08:50:00": "126", "09:10:00": "128", "09:30:00": "130", "09:50:00": "132", "10:10:00": "134", "10:30:00": "136", "10:50:00": "138", "11:10:00": "140", "11:30:00": "142", "11:50:00": "144", "12:10:00": "002", "12:30:00": "004", "12:50:00": "006", "13:10:00": "008", "13:30:00": "010", "13:50:00": "012", "14:10:00": "014", "14:30:00": "016", "14:50:00": "018", "15:10:00": "020", "15:30:00": "022", "15:50:00": "024", "16:10:00": "026", "16:30:00": "028", "16:50:00": "030", "17:10:00": "032", "17:30:00": "034", "17:50:00": "036", "18:10:00": "038", "18:30:00": "040", "18:50:00": "042", "19:10:00": "044", "19:30:00": "046", "19:50:00": "048", "20:10:00": "146", "20:30:00": "148", "20:50:00": "150", "21:10:00": "152", "21:30:00": "154", "21:50:00": "156", "22:30:00": "158", "23:30:00": "160"}
FormatTime, routetime,, HHmmss
routetime := A_YYYY . A_MM . A_DD . routetime
prevDiff := 0xFFFFFFFF
for k, v in routes {
   if SubStr(routetime, 9, 2) = "23" && k = "00:00:00"
      k := "23:59:59"
   time := A_YYYY . A_MM . A_DD . StrReplace(k, ":")
   now := A_Now
   now -= time, s
   if (now >= 0)
      continue
   diff := routetime
   diff -= time, s
   if ( Abs(diff) < prevDiff )
      route := v, prevDiff := Abs(diff)
}
MsgBox, % route
Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Skype dmitry_fiveg