Jump to content

Recommended Posts

Posted (edited)

Dear all,

Here is code with problems and requsts to modify it

#include <Array.au3>
#include <StringConstants.au3>

Local $sValue = "'133.0.3','133.0','133.0b7','133.0b8','133.0b9','134.0.1','134.0.2','134.0','134.0b1','134.0b3','134.0b5','134.0b9','135.0','135.0.1','135.0.2','135.0b1','135.0b3','135.0b5','135.0b9','136.0b1'"

_ArraySort($sValue, 1)
$aExtractDigits1 = StringRegExp($sValue, "\d+\.\d+", 3) ; Get an array of all digit groups
$aExtractDigits2 = StringRegExp($sValue, "\d+\.\d+.\d+", 3) ; Get an array of all digit groups
$bExtractDigits1 = _ArrayMax($aExtractDigits1, 1)
$bExtractDigits2 = _ArrayMax($aExtractDigits2, 1)
MsgBox($MB_SYSTEMMODAL, '11111111',  $bExtractDigits1)  ; >>> Returns result = 136.0
MsgBox($MB_SYSTEMMODAL, '22222222',  $bExtractDigits2)  ; >>> Returns result = 136.0b1
Exit

; +===============>>> Requests :
; 1- I want stable numbers + digits & excluse other values with letter b as it's Beta version
; 2- I want Maximum numbers + digits as it is the actual latest version, among all values the stable latest version is : 135.0.2
; but my code did not return to it

 

Requests :

1- I want stable numbers + digits & exclude other values with letter b as it's Beta version
2- I want Maximum numbers + digits as it is the actual latest version

Among all values in the code the stable latest version is :  135.0.2  but my code did not return to it

3- I need the code to return to 135.0.2 as it is the maximum value as other values contain letter : b indicating beta version

Problems in the code:

the code returns to 136.0 (part of beta version value) and also return to 136.0b1 

Edited by Davidyese
Posted

1. To extract only the version numbers without beta or anything else, you can use the following pattern (there are many alternative approaches for such a pattern, but this is based on your specific example):

(?>\d+\.)+\d+\b

2. A normal _ArrayMax/_ArraySort etc. is not sufficient to determine the actual maximum including all secondary versions. The elements are strings and a classic AutoIt string comparison, on which _ArrayMax/_ArraySort is based, compares character by character. In this specific case, this would even work, but if you imagine that you now have the elements '135.0.3' and '135.0.20' in the list, then '135.0.3' would still be returned as the result.
You have tried to get around this by setting the size comparison for _ArrayMax to “numeric”, but that doesn't help here either, as it means that the strings are first converted into a number. And that means that “135.0.2” simply becomes 135.

So you either need a function that sorts the array with its own comparison function or returns the maximum, or you create a value for each value yourself that can be correctly compared by _ArrayMax.

For the former, there are already UDFs such as >>this<<. The solution to your question would look like this:

#include "ArrayPlus.au3"

; input string
$sValue = "'133.0.3','133.0','133.0b7','133.0b8','133.0b9','134.0.1','134.0.2','134.0','134.0b1','134.0b3','134.0b5','134.0b9','135.0','135.0.1','135.0.2','135.0b1','135.0b3','135.0b5','135.0b9','136.0b1'"

; extract stable versions
$aStables = StringRegExp($sValue, "(?>\d+\.)+\d+\b", 3)

; determine max version number
$sMax = _ArrayGetMax($aStables, __ap_cb_comp_Natural)

; present result
MsgBox(0,"max stable version", $sMax)

3. _ArraySort($sValue, 1) does nothing at all. As the name suggests, it sorts arrays. $sValue, on the other hand, is a string. (What @error also tells you, if it had been analyzed.)

Posted (edited)

Hi @Davidyese 👋 ,

here a different variant which I use in a project. Of course I adjusted the non stable version indicator for your case (beta "b") a bit. See foot note comments.

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

;~ #include-once
;~ #include <Array.au3>

_Main()

Func _Main()
    Local Const $sValue = "'133.0.3','133.0','133.0b7','133.0b8','133.0b9','134.0.1','134.0.2','134.0','134.0b1','134.0b3','134.0b5','134.0b9','135.0','135.0.1','135.0.2','135.0b1','135.0b3','135.0b5','135.0b9','136.0b1'"

    Local Const $iZeroBasedFlag = 2
    Local Const $aVersionList = StringSplit(StringReplace($sValue, "'", ''), ',', $iZeroBasedFlag)

    ;~ _ArrayDisplay($aVersionList)

    ConsoleWrite(_GetLatestStableVersion($aVersionList) & @CRLF)
EndFunc

Func _GetLatestStableVersion($aVersionList)
    Local Const $iCount = UBound($aVersionList) - 1
    Local Const $sBetaIndicator = 'b' ; *

    Local $sVersion
    Local $sLatestStableVersion = '0.0.0'

    For $i = 0 To $iCount
        $sVersion = $aVersionList[$i]

        If StringInStr($sVersion, $sBetaIndicator) Then ; **
            ContinueLoop
        EndIf

        If _IsVersionNewer($sLatestStableVersion, $sVersion) Then
            $sLatestStableVersion = $sVersion
        EndIf
    Next

    Return $sLatestStableVersion

    ;~ *, **:
    ;~ This could also be a RegEx pattern and StringRegExp()
    ;~ as non valid indicator for a stable version.
EndFunc

Func _IsVersionNewer($sCurrentLatestStableVersion, $sComparedVersion)
    $sCurrentLatestStableVersion = _EnsureSemVerFormat($sCurrentLatestStableVersion)
    $sComparedVersion            = _EnsureSemVerFormat($sComparedVersion)

    Local Const $aCurrentLatestStableVersion = StringSplit($sCurrentLatestStableVersion, '.')
    Local Const $aComparedVersion            = StringSplit($sComparedVersion, '.')
    Local $iLTSPart, $iComparedPart

    For $i = 1 To 3
        $iLTSPart      = Number($aCurrentLatestStableVersion[$i])
        $iComparedPart = Number($aComparedVersion[$i])

        If $iLTSPart < $iComparedPart Then
            Return True
        ElseIf $iLTSPart > $iComparedPart Then
            Return False
        EndIf
    Next

    Return False
EndFunc

Func _EnsureSemVerFormat($sVersion)
    ; Expected version formart is "Major.Minor.Patch".
    ; https://semver.org/
    ;
    ; If the version does not have Minor or Patch,
    ; then it will be added as 0.

    StringReplace($sVersion, '.', '')
    Switch @extended
        Case 0
            Return $sVersion & '.0.0'
        Case 1
            Return $sVersion & '.0'
        Case Else
            Return $sVersion
    EndSwitch
EndFunc


By the way: Very very good (as usually) @AspirinJunkie 😀 .

The main difference to your more mature approach is that my return value will alway return a "Major.Minor.Patch" version string instead of the input version string. That means if 136 or 136.2 are the input version, the output would be 136.0.0 or 136.2.0 to fulfill SemVer syntax. I don't know whether this is a problem for you @Davidyese or not?

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   1 member

×
×
  • Create New...