|
|
|
| 25 March 2002 | CompareVersions() |
| Author | ScriptLogic Corporation |
| Action | Compares two multi-segment version strings. |
| Syntax | CompareVersions (Ver1, Ver2, [Limit]) |
| Parameters | |
| Remarks | *** Look at the new VersionCompare() UDF -- you may find it easier understand and use. *** Allows either a "." or "," as segment separator. Limited to 4 segments per version string. Limited to 4 digits per segment. Neither the number of segments nor the length of each segment in each version string need not be identical. (i.e. a version of "4" will match a version of "4.00" and "5.1" will match "5.10") |
| Returns | -2 One or more versions strings were not supplied. -1 First version string is older than the second version string. 0 Version strings are the same. 1 First version string is newer that the second version string. |
| Dependencies | None. |
| Examples |
;Example1
$Version1='4'
$Version2='4.00'
$result=CompareVersions($Version1,$Version2)
select
case $result=0
? 'The versions are the same'
case $result=1
? '$Version1 is newer than $Version2'
case $result=-1
? '$Version1 is older than $Version2'
endselect
;Example2
$Version1 = '5.00.2920.0000' ; IE 5.01 w/o any SP
$Version2 = '5.00.3105.0106' ; IE 5.01 w/ SP1
;reference: '5.00.3314.2101' ; IE 5.01 w/ SP2
$result=CompareVersions($Version1,$Version2)
select
case $result=0
? 'The versions are the same'
case $result=1
? '$Version1 is newer than $Version2'
case $result=-1
? '$Version1 is older than $Version2'
endselect
|
| Source |
FUNCTION CompareVersions ($ver1, $ver2, OPTIONAL $limit)
DIM $ver1sep, $ver2sep, $ver1a, $ver2a, $ver1segs, $ver2segs
DIM $lenseg, $segindex, $zeros
IF 0+$limit = 0
$limit=4
ENDIF
IF ($ver1 = '') OR ($ver2 = '') ; not all params supplied
$compareversions=-2
RETURN
ENDIF
IF ($ver1 = $ver2) ; quick out for exact matches
$compareversions=0
RETURN
ENDIF
IF InStr($ver1,',') ; determine Ver1 separator character
$ver1sep=','
ELSE
$ver1sep='.'
ENDIF
IF InStr($ver2,',') ; determine Ver2 separator character
$ver2sep=','
ELSE
$ver2sep='.'
ENDIF
$zeros='0000'
$ver1=$ver1+$ver1sep+$ver1sep+$ver1sep+$ver2sep ; pad
$ver2=$ver2+$ver2sep+$ver2sep+$ver2sep+$ver2sep ; pad
$ver1a=Split($ver1,$ver1sep)
REDIM PRESERVE $ver1a[$limit-1]
$ver2a=Split($ver2,$ver2sep)
REDIM PRESERVE $ver2a[$limit-1]
$compareversions=0
FOR $segindex = 0 TO $limit-1
; need to pad elements so that 5.1=5.10 & 4=4.0
$lenseg=Len(''+$ver1a[$segindex])
IF ($lenseg < 4)
$ver1a[$segindex]=''+$ver1a[$segindex]+Substr($zeros,1,4-$lenseg)
ENDIF
$lenseg=Len(''+$ver2a[$segindex])
IF ($lenseg < 4)
$ver2a[$segindex]=''+$ver2a[$segindex]+Substr($zeros,1,4-$lenseg)
ENDIF
; Now that segments are padded, determine which is newer...
SELECT
CASE 0+$ver1a[$segindex] > 0+$ver2a[$segindex]
$compareversions=1
EXIT
CASE 0+$ver1a[$segindex] < 0+$ver2a[$segindex]
$compareversions=-1
EXIT
CASE 1
; segments are equal
ENDSELECT
NEXT
ENDFUNCTION ; - CompareVersions -
|
|
|
|