scripting@wanadoo.nl




VersionCompare()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
1 April 2002 VersionCompare()
AuthorScriptLogic Corporation
Action Compare two multi-segment version strings.
SyntaxVersionCompare (Version1, Comparison, Version2, Limit) 
Parameters
  • Version1 (Required / String) The first version string.
  • Comparison (Required / String) The comparison to be performed on the two version strings.
  • Version2 (Required / String) The second version string.
  • Limit (Optional / Numeric) Optional parameter that can limit how may segments of each version string are compared to one another. 
  • RemarksThis function was designed as a replacement for the CompareVersions() UDF. It is easier to use and understand.

    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 1 = Comparison of the version strings evaluates true.
    0 = Comparison of the version strings evaluates false.
    "" = All required parameters were not supplied. 
    DependenciesJoin() 
    Examples
    IF VersionCompare($IeCurVer,'<','6.0')
       ; update IE to 6.0
    ELSE
       ? ' You already have IE 6.0'
    ENDIF
    
     
    Source
    FUNCTION VersionCompare ($ver1, $comparison, $ver2, OPTIONAL $limit)
      DIM $ver1sep, $ver2sep, $ver1a, $ver2a, $segindex
      $limit=0+$limit ; cast numeric
      IF ($limit = 0) OR ($limit > 4)
        $limit=4
      ENDIF
      $ver1=''+$ver1 ; cast string
      $ver2=''+$ver2 ; cast string
      IF ($ver1 = '') OR ($ver2 = '') OR ($comparison = '') ; not all params supplied
        $versioncompare=''
        RETURN
      ENDIF
      ; determine Ver1 separator character for split
      IF InStr($ver1,',')
        $ver1sep=','
      ELSE
        $ver1sep='.'
      ENDIF
      ; determine Ver2 separator character for split
      IF InStr($ver2,',')
        $ver2sep=','
      ELSE
        $ver2sep='.'
      ENDIF
      ; add segments, so that we can then restrict to four.
      $ver1=$ver1+$ver1sep+$ver1sep+$ver1sep+$ver2sep
      $ver2=$ver2+$ver2sep+$ver2sep+$ver2sep+$ver2sep
      ; split segments out into array elements
      $ver1a=Split($ver1,$ver1sep)
      REDIM PRESERVE $ver1a[$limit-1]
      $ver2a=Split($ver2,$ver2sep)
      REDIM PRESERVE $ver2a[$limit-1]
      ; pad first segment element with leading zeros
      $ver1a[0]=Right(''+'0000'+$ver1a[0],4)
      $ver2a[0]=Right(''+'0000'+$ver2a[0],4)
      ; pad remaining segment elements with trailing zeros
      FOR $segindex = 1 TO $limit-1
        $ver1a[$segindex]=Left(''+Trim($ver1a[$segindex])+'0000',4)
        $ver2a[$segindex]=Left(''+Trim($ver2a[$segindex])+'0000',4)
      NEXT
      ; rejoin array elements into single stings with common separators
      $ver1=Join($ver1a,'.')
      $ver2=Join($ver2a,'.')
      ; Now that segments are padded, determine if comparison is true
      SELECT
        CASE InStr($comparison,'=') AND ($ver1 = $ver2)
          $versioncompare=1
        CASE InStr($comparison,'<') AND ($ver1 < $ver2)
          $versioncompare=1
        CASE InStr($comparison,'>') AND ($ver1 > $ver2)
          $versioncompare=1
        CASE 1 ; false
          $versioncompare=0
      ENDSELECT
    ENDFUNCTION ; - VersionCompare -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter