Page 1 of 1 1
Topic Options
#203139 - 2011-09-26 10:15 PM Problems with CompareVersions() and CompareVerString()
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
I've been using CompareVersions() for some time now and never really noticed any problems. However, today I was comparing the last version of Adobe Flash, 10.1.183.7 to the current version which is 10.1.183.10. CompareVersions was returning that the .7 is newer than the .10 version. The reason is because of the 0 buffering. Since .10 has two places and .7 only has one, a zero is buffered to the right of .7 creating a value .70, which is higher in value, but incorrect. Checking CompareVerString() returns the same faulty value. I played around with this, but I really couldn't come up with a good working fix. Any ideas?

CompareVersions - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=82864#Post82864
CompareVerString - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=196541#Post196541

@Glenn, you might consider adding the following code to yours, so that it separates on commas too.

 Code:
  if instr($_Version1,",")
    $_Version1=Join(Split($_Version1,","),".")
  endif
  if instr($_Version2,",")
    $_Version2=Join(Split($_Version2,","),".")
  endif

Top
#203140 - 2011-09-26 10:44 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Try this modded version of CompareVerString()...I got it to work for me by simply adding Val() around the values when they are compared.

 Code:
Function CompareVerString($_Version1, $_Version2)
 
  Dim $_X, $_Max, $_Val, $_Wt			; Index, Max, Value & Weight vars 
  Dim $_Err					; error code 
  Dim $_Vf1, $_Vf2				; version field counts 
 
  $CompareVerString = 0				; Default to match 
 
  $_Version1 = Split($_Version1, '.')		; get version as array of elements 
  $_Version2 = Split($_Version2, '.')
 
  $_Vf1 = UBound($_Version1)			; elements in version1 array 
  $_Vf2 = UBound($_Version2)			; elements in version2 array 
 
  ; insure that both version strings have the same number 
  ; of fields - missing fields are set to 0. 
  Select					; find highest value 
   Case $_Vf1 = $_Vf2
    $_Max = $_Vf1
   Case $_Vf1 < $_Vf2
    $_Max = $_Vf2
    ReDim Preserve $_Version1[$_Max]		; adjust array and init new values 
    For $_X = ($_Vf1 + 1) to $_Max $_Version1[$_X] = 0 Next
   Case $_Vf1 > $_Vf2
    $_Max = $_Vf1
    ReDim Preserve $_Version2[$_Max]		; adjust array and init new values 
    For $_X = ($_Vf2 + 1) to $_Max $_Version2[$_X] = 0 Next
  EndSelect
 
  ; Compare each element pair in the version strings, starting with the least-significant 
  ; If the element values are equal, they are effictively ignored. If the desired value 
  ; is greater than the installed value, the current Match Value is added to the weight, but 
  ; if the desired value is less than the installed value, the current Match Value is  
  ; subtracted from the weight. The Match Value is increased in binary form as the  
  ; significance of the version values increases. This allows mis-matched string comparisons, 
  ; such as "3.0" and "2.46.24.5", to be processed correctly. 
  $_Val = 1					; current match value 
  $_Wt = 0					; match weight 

  For $_X = $_Max to 0 Step -1
    Select
     Case Val($_Version1[$_X]) < Val($_Version2[$_X])
      $_Wt = $_Wt + ($_Val * -1)
     Case Val($_Version1[$_X]) > Val($_Version2[$_X])
      $_Wt = $_Wt + $_Val
    EndSelect
    $_Val = $_Val * 2
 Next
 
  Select
   Case $_Wt > 0
    $CompareVerString = -1			; Version 2 < Version1 
   Case $_Wt = 0
    $CompareVerString = 0			; Version 2 = Version1 
   Case $_Wt < 0
    $CompareVerString = 1			; Version 2 > Version1 
  EndSelect
 
  Exit 0
 
EndFunction

Top
#203141 - 2011-09-26 10:55 PM Re: Problems with CompareVersions() and CompareVerString() [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
You can also try the modified shorter version that Richard H. posted on the compareverstring() post. You'll find that it also works, since he put CInt() around the values.
Top
#203142 - 2011-09-26 10:59 PM Re: Problems with CompareVersions() and CompareVerString() [Re: ShaneEP]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
hmmm...

 Code:
? compareverstring("10.1.183.7","10.1.183.10")
? compareverstring("10.1.183.10","10.1.183.7")


with original code... -1 for either
with mod code ... 1 for either

Top
#203143 - 2011-09-26 11:01 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
I'll check out Richard's version... I missed that (using the UDF repository)
Top
#203144 - 2011-09-26 11:01 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Weird...Try this one then. It works with your above example. Returns 1 and -1.

 Code:
Function CVS($_Version1, $_Version2)
 
  Dim $_X, $_Max
 
  $CVS = 0
 
  $_Version1 = Split($_Version1, '.')
  $_Version2 = Split($_Version2, '.')
 
  $_Max=IIf(UBound($_Version1)>UBound($_Version2),UBound($_Version1),UBound($_Version2))
  ReDim Preserve $_Version1[$_Max]
  ReDim Preserve $_Version2[$_Max]

  For $_X = 0 to $_Max
    Select
     Case CInt($_Version1[$_X]) < CInt($_Version2[$_X])
      $CVS=1  Exit 0
     Case CInt($_Version1[$_X]) > CInt($_Version2[$_X])
      $CVS=-1 Exit 0
    EndSelect
  Next
 
  Exit 0

EndFunction

Top
#203145 - 2011-09-26 11:05 PM Re: Problems with CompareVersions() and CompareVerString() [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Actually...I get expected results for both UDFs...The following returns..

1
-1

1
-1
 Code:
? cvs("10.1.183.7","10.1.183.10")
? cvs("10.1.183.10","10.1.183.7")
?
? compareverstring("10.1.183.7","10.1.183.10")
? compareverstring("10.1.183.10","10.1.183.7")

get $

Function CVS($_Version1, $_Version2)
  Dim $_X, $_Max
  $CVS = 0
  $_Version1 = Split($_Version1, '.')
  $_Version2 = Split($_Version2, '.')
  $_Max=IIf(UBound($_Version1)>UBound($_Version2),UBound($_Version1),UBound($_Version2))
  ReDim Preserve $_Version1[$_Max]
  ReDim Preserve $_Version2[$_Max]
  For $_X = 0 to $_Max
    Select
     Case CInt($_Version1[$_X]) < CInt($_Version2[$_X])
      $CVS=1  Exit 0
     Case CInt($_Version1[$_X]) > CInt($_Version2[$_X])
      $CVS=-1 Exit 0
    EndSelect
  Next
  Exit 0
EndFunction

Function CompareVerString($_Version1, $_Version2)
  Dim $_X, $_Max, $_Val, $_Wt			; Index, Max, Value & Weight vars 
  Dim $_Err					; error code 
  Dim $_Vf1, $_Vf2				; version field counts 
  $CompareVerString = 0				; Default to match 
  $_Version1 = Split($_Version1, '.')		; get version as array of elements 
  $_Version2 = Split($_Version2, '.')
  $_Vf1 = UBound($_Version1)			; elements in version1 array 
  $_Vf2 = UBound($_Version2)			; elements in version2 array 
  ; insure that both version strings have the same number 
  ; of fields - missing fields are set to 0. 
  Select					; find highest value 
   Case $_Vf1 = $_Vf2
    $_Max = $_Vf1
   Case $_Vf1 < $_Vf2
    $_Max = $_Vf2
    ReDim Preserve $_Version1[$_Max]		; adjust array and init new values 
    For $_X = ($_Vf1 + 1) to $_Max $_Version1[$_X] = 0 Next
   Case $_Vf1 > $_Vf2
    $_Max = $_Vf1
    ReDim Preserve $_Version2[$_Max]		; adjust array and init new values 
    For $_X = ($_Vf2 + 1) to $_Max $_Version2[$_X] = 0 Next
  EndSelect
  ; Compare each element pair in the version strings, starting with the least-significant 
  ; If the element values are equal, they are effictively ignored. If the desired value 
  ; is greater than the installed value, the current Match Value is added to the weight, but 
  ; if the desired value is less than the installed value, the current Match Value is  
  ; subtracted from the weight. The Match Value is increased in binary form as the  
  ; significance of the version values increases. This allows mis-matched string comparisons, 
  ; such as "3.0" and "2.46.24.5", to be processed correctly. 
  $_Val = 1					; current match value 
  $_Wt = 0					; match weight 
  For $_X = $_Max to 0 Step -1
    Select
     Case Val($_Version1[$_X]) < Val($_Version2[$_X])
      $_Wt = $_Wt + ($_Val * -1)
     Case Val($_Version1[$_X]) > Val($_Version2[$_X])
      $_Wt = $_Wt + $_Val
    EndSelect
    $_Val = $_Val * 2
  Next
  Select
   Case $_Wt > 0
    $CompareVerString = -1			; Version 2 < Version1 
   Case $_Wt = 0
    $CompareVerString = 0			; Version 2 = Version1 
   Case $_Wt < 0
    $CompareVerString = 1			; Version 2 > Version1 
  EndSelect
  Exit 0
EndFunction

Not sure where our differences are coming from.

Top
#203146 - 2011-09-26 11:06 PM Re: Problems with CompareVersions() and CompareVerString() [Re: ShaneEP]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
I may have introduced something in that is causing those results to be like that... need to roll back. Richards seems to be working right though.
Top
#203147 - 2011-09-26 11:23 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
So just to be clear, are you seeing that comparverstrings is returning the wrong value, unless it has the val mods you added?
Top
#203148 - 2011-09-26 11:42 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Okay, here are my results after retesting.

It appears that CVS and the modified version of CompareVerString return the correct value, and the original CompareVerString and CompareVersions are returning the incorrect value. (CompareVersions inputs are reversed so I flipped the inputs below...)

 Code:
? cvs("10.1.183.7","10.1.183.10") ; returns 1
? cvs("10.1.183.10","10.1.183.7") ; returns -1
?
? compareverstringMOD("10.1.183.7","10.1.183.10")  ; returns 1
? compareverstringMOD("10.1.183.10","10.1.183.7")  ; returns -1
?
? compareverstring("10.1.183.7","10.1.183.10") ; returns -1
? compareverstring("10.1.183.10","10.1.183.7") ; returns 1
?
? compareversions("10.1.183.10","10.1.183.7") ; returns -1
? compareversions("10.1.183.7","10.1.183.10") ; returns 1

Top
#203149 - 2011-09-27 03:55 AM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Yes I also got the incorrect results with the original compareverstring(). I never tried compareversions().
Top
#203150 - 2011-09-27 05:50 PM Re: Problems with CompareVersions() and CompareVerString() [Re: ShaneEP]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Using Val() in the comparisons in CompareVerString will break the logic, as this is designed to perform string comparisons. This allows the use of alpha modifiers in the version string, such as "2.3b".

The UDF has been updated with the copy of v1.1 from our web site. Always look at the Kix UDF Library on our web site for the latest versions of UDFs that we write. Our live development library is postprepped and updated every night at 22:00 EST.

The header clearly states that the version string is "." delimited - if you need to alter that, you should change the strings before calling the UDF.

Glenn

PS - The current UDF used the following examples for validation
 Code:
break on
'7:10 '
compareverstring("10.1.183.7","10.1.183.10")
?
'10:7 '
compareverstring("10.1.183.10","10.1.183.7")
?


'10:10 '
compareverstring("10.1.183.10","10.1.183.10")
?


'10a:10a '
compareverstring("10.1.183.10a","10.1.183.10a")
?

'10a:10 '
compareverstring("10.1.183.10a","10.1.183.10")
?
'10:10a '
compareverstring("10.1.183.10","10.1.183.10a")
?

'10a:10b '
compareverstring("10.1.183.10a","10.1.183.10b")
?
'10b:10a '
compareverstring("10.1.183.10b","10.1.183.10a")
?
_________________________
Actually I am a Rocket Scientist! \:D

Top
#203152 - 2011-09-27 06:48 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Thanks for the update. For whatever reason, 1.0 is still here even though it says you made an edit today for version 1.1.
Top
#203153 - 2011-09-27 07:09 PM Re: Problems with CompareVersions() and CompareVerString() [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Strange, I even previewed it!

It's there now!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 525 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.049 seconds in which 0.013 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org