string version number comparison Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

string version number comparison

08 Nov 2019, 12:49

so AHK's string comparison usually is sufficient, until its not

Code: Select all

;works
MsgBox, % (A_AhkVersion < "1.1.31.01")

;doesnt work
MsgBox, % ("8.1" < "10")
would it be sufficient to StrSplit each part between the dots, and then add 0s padding? so that it becomes:

Code: Select all

;works
MsgBox, % ("08.1" < "10")
then will the string comparison always work?
just wondering if that's the only consideration, or if i'm missing some other cases

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: string version number comparison

08 Nov 2019, 13:45

Hi :wave: .
In this case you can save the strings to variables and do numeric comparisons.

Cheers.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: string version number comparison

08 Nov 2019, 13:50

Helgef wrote:
08 Nov 2019, 13:45
Hi :wave: .
In this case you can save the strings to variables and do numeric comparisons.

Cheers.
numeric comparison will only work for one decimal point. version strings can have endless, like 1.1.31.01 ;)

i already use VersionCompare() from here which splits each part and then numerically compares each part. thats probably the best way, but i like the simplicity of just using <> operators so was thinking of doing something like:

Code: Select all

if (pad("8.1") < pad("10"))
;rather than
if (VersionCompare("8.1", "10") = 2)
Last edited by guest3456 on 08 Nov 2019, 13:54, edited 2 times in total.

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: string version number comparison

08 Nov 2019, 13:54

There are some version compare functions here.

Edit: Didn't see the edit above (wont let me delete this post!).
Last edited by boiler on 08 Nov 2019, 13:56, edited 2 times in total.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: string version number comparison

08 Nov 2019, 13:54

boiler wrote:
08 Nov 2019, 13:54
There are some version compare functions here.
yes i know see my edit above

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: string version number comparison

08 Nov 2019, 16:11

@guest3456

Code: Select all

MsgBox, % pad("8.1") < pad("10")

pad(ver) {
   str := RegExReplace(ver, "(?<=\.)\d(?=\.|$)", "0$0")
   Return RegExReplace(str, "(?<!^\d)(?<!^\d\d)\.")
}
Like this?
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: string version number comparison

08 Nov 2019, 19:40

teadrinker wrote:
08 Nov 2019, 16:11
@guest3456

Code: Select all

MsgBox, % pad("8.1") < pad("10")

pad(ver) {
   str := RegExReplace(ver, "(?<=\.)\d(?=\.|$)", "0$0")
   Return RegExReplace(str, "(?<!^\d)(?<!^\d\d)\.")
}
Like this?
no i dont think so.. but i just realized that there is no way for the function to know how many digits to pad.. how many 0's to pre-pend at the start. probably best to just stick with my existing func

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: string version number comparison

08 Nov 2019, 20:17

guest3456 wrote: i just realized that there is no way for the function to know how many digits to pad
You could pad with an excess number of zeroes:

Code: Select all

MsgBox, % pad("1.1.31.9") < pad("1.1.31.0123")

pad(ver) {
   Loop, parse, ver, .
      str .= (str = "" ? "" : ".") . Format("{:010}", A_LoopField)
   Return str
}
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: string version number comparison

08 Nov 2019, 21:21

teadrinker wrote:
08 Nov 2019, 20:17
guest3456 wrote: i just realized that there is no way for the function to know how many digits to pad
You could pad with an excess number of zeroes:

Code: Select all

MsgBox, % pad("1.1.31.9") < pad("1.1.31.0123")

pad(ver) {
   Loop, parse, ver, .
      str .= (str = "" ? "" : ".") . Format("{:010}", A_LoopField)
   Return str
}
nice yeah that looks like it would work. everything is 10 digits long and so string comparison would work

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: string version number comparison  Topic is solved

09 Nov 2019, 02:05

A minor fix:

Code: Select all

MsgBox, % pad("1.0") = pad("1.0.0")

pad(ver) {
   arr := StrSplit(ver, ".")
   Loop 4
      (arr[A_Index] = "" && arr[A_Index] := 0)
   Return Format("{:010}.{:010}.{:010}.{:010}", arr*)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Oblomov228, uchihito and 167 guests