Jump to content

string increment alphabetic to numeric or viceversa


Recommended Posts

i'm tring to write a func that will take a text and increment the last letter by one. and if it is alphabetic it will go to numeric and viceversa.

for example if i have ABZ ==> AB0 and if it is AB9 ==>ACA.

i have searched everywhere but all i fund is increment of alphabetic.

i fund this but didn't manage to figure out how to change it.

Local $sMyString = "ZZZX"
For $i = 1 To 5
    $sMyString = _AlphaIncr($sMyString)
    ConsoleWrite($sMyString & @TAB & (@error ? "<- Overflow!" : "") & @CRLF)
Next


; returns the passed string incremented by 1 asc
; set @error on overflow
Func _AlphaIncr($sString)
    Local $sResult = "", $Carry = 1
    For $i = StringLen($sString) To 1 Step -1
        $sResult = Chr(Mod((Asc(StringMid($sString, $i, 1)) - 65 + $Carry), 26) + 65) & $sResult
        $Carry = Mod((Asc(StringMid($sString, $i, 1)) + $Carry), 91) <> (Asc(StringMid($sString, $i, 1)) + $Carry)
    Next
    Return SetError($Carry, 0, $sResult)
EndFunc   ;==>_AlphaIncr

 

Link to comment
Share on other sites

This modification will do what you want, and also handles input in upper or lowercase converting the overflow into the correct case depending upon what was input.

Local $sMyString = "ZwZX"
For $i = 1 To 5
    $sMyString = _AlphaIncr($sMyString)
    ConsoleWrite($sMyString & @TAB & (@error ? "<- Overflow!" : "") & @CRLF)
Next


; returns the passed string incremented by 1 asc
Func _AlphaIncr($sString)
    Local $sResult = "", $Carry = ""
    For $i = 1 To StringLen($sString)
        $iStringAdd = Asc(StringMid($sString, $i, 1)) + 1
        Switch $iStringAdd
            Case 91, 123
                $iStringAdd = 48
            Case 58
                If $iStringAdd - 1 = 90 Then
                    $iStringAdd = 65
                ElseIf $iStringAdd - 1 = 122 Then
                    $iStringAdd = 97
                EndIf
        EndSwitch
        $sResult = Chr($iStringAdd)
        $Carry &= $sResult
    Next
    Return $Carry
EndFunc   ;==>_AlphaIncr

This gives an output like this:

0x0y    
1y1z    
2z20    
3031    
4142    
 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Since it is only the last char that needs to be incremented, you could use something like this :

Local $sText = "ABCT"

For $i = 0 To 35
  $sText = _IncrLastChar ($sText)
  ConsoleWrite ($sText & @CRLF)
Next

Func _IncrLastChar ($sString)

  Local $iLen = StringLen ($sString)
  Local $iLast = Asc(StringRight ($sString,1))+1
  $iLast = ($iLast = 58 ? 65 : ($iLast = 91 ? 48 : $iLast))
  Return StringLeft ($sString,$iLen-1) & Chr ($iLast)

EndFunc

 

Link to comment
Share on other sites

14 minutes ago, BrewManNH said:

This modification will do what you want, and also handles input in upper or lowercase converting the overflow into the correct case depending upon what was input.

Local $sMyString = "ZwZX"
For $i = 1 To 5
    $sMyString = _AlphaIncr($sMyString)
    ConsoleWrite($sMyString & @TAB & (@error ? "<- Overflow!" : "") & @CRLF)
Next


; returns the passed string incremented by 1 asc
Func _AlphaIncr($sString)
    Local $sResult = "", $Carry = ""
    For $i = 1 To StringLen($sString)
        $iStringAdd = Asc(StringMid($sString, $i, 1)) + 1
        Switch $iStringAdd
            Case 91, 123
                $iStringAdd = 48
            Case 58
                If $iStringAdd - 1 = 90 Then
                    $iStringAdd = 65
                ElseIf $iStringAdd - 1 = 122 Then
                    $iStringAdd = 97
                EndIf
        EndSwitch
        $sResult = Chr($iStringAdd)
        $Carry &= $sResult
    Next
    Return $Carry
EndFunc   ;==>_AlphaIncr

This gives an output like this:

0x0y    
1y1z    
2z20    
3031    
4142    
 

thanks for the replay.

but it's not like i want. i want when it get to Z it goes to 0 and when it get to 9 i goes to AA.

Link to comment
Share on other sites

11 minutes ago, Nine said:

Since it is only the last char that needs to be incremented, you could use something like this :

Local $sText = "ABCT"

For $i = 0 To 35
  $sText = _IncrLastChar ($sText)
  ConsoleWrite ($sText & @CRLF)
Next

Func _IncrLastChar ($sString)

  Local $iLen = StringLen ($sString)
  Local $iLast = Asc(StringRight ($sString,1))+1
  $iLast = ($iLast = 58 ? 65 : ($iLast = 91 ? 48 : $iLast))
  Return StringLeft ($sString,$iLen-1) & Chr ($iLast)

EndFunc

 

thanks for the replay.

but again it's not what i want.

i want the string go from AAAA to 9999 at the end.

Link to comment
Share on other sites

13 minutes ago, ali_lbay said:

i want when it get to Z it goes to 0

This script does that.

Quote

and when it get to 9 i goes to AA.

You never stated that you wanted it to go from 9 to AA. 

I had to modify the script above, and remove the attempt at keeping case sensitivity which clearly didn't work as intended. 

Local $sMyString = "ZwZx"
For $i = 1 To 35
    $sMyString = _AlphaIncr($sMyString)
    ConsoleWrite($sMyString & @TAB & (@error ? "<- Overflow!" : "") & @CRLF)
Next


; returns the passed string incremented by 1 asc
Func _AlphaIncr($sString)
    Local $sResult = "", $Carry = ""
    For $i = 1 To StringLen($sString)
        $iStringAdd = Asc(StringMid($sString, $i, 1)) + 1
        Switch $iStringAdd
            Case "91", "123"
                $iStringAdd = 48
            Case "58"
                $iStringAdd = 65
        EndSwitch
        $sResult = Chr($iStringAdd)
        $Carry &= $sResult
    Next
    Return $Carry
EndFunc   ;==>_AlphaIncr

This will give this as an output.

0x0y    
1y1z    
2z20    
3031    
4142    
5253    
6364    
7475    
8586    
9697    
A7A8    
B8B9    
C9CA    
DADB    
EBEC    
FCFD    
GDGE    
HEHF    
IFIG    
JGJH    
KHKI    
LILJ    
MJMK    
NKNL    
OLOM    
PMPN    
QNQO    
RORP    
SPSQ    
TQTR    
URUS    
VSVT    
WTWU    
XUXV    
YVYW    
 

 

If you want it to output AA after being incremented from 9, I'll leave that part to you. ;)

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Everyone seems to be missing the point here... its only the last value that's being incremented, not each value within the string.  Kind of like counting in your own base(x), where x can be any set of values in any order.  A fun little project.  Code below would work with any set of chars, I've added your A-Z,0-9 as example, but you could use base(!@#$%^&*()) if you wanted.

#Include <Array.au3>

Global $aChars[0]
CreateCharArray()


$ret = _IncrementString("8999")
If Not $ret Then
    MsgBox(0,'',"Error! Character #" & @Extended & " does not exist in Chars array!")
Else
    Msgbox(0,'',"Incremented value is: " & $ret)
EndIf


$ret = _IncrementString("9999")
If Not $ret Then
    MsgBox(0,'',"Error! Character #" & @Extended & " does not exist in Chars array!")
Else
    Msgbox(0,'',"Incremented value is: " & $ret)
EndIf


$ret = _IncrementString("A9")
If Not $ret Then
    MsgBox(0,'',"Error! Character #" & @Extended & " does not exist in Chars array!")
Else
    Msgbox(0,'',"Incremented value is: " & $ret)
EndIf

$ret = _IncrementString("A@")
If Not $ret Then
    MsgBox(0,'',"Error! Character #" & @Extended & " does not exist in Chars array!")
Else
    Msgbox(0,'',"Incremented value is: " & $ret)
EndIf


Func CreateCharArray()
    For $iChr = 65 to 90
        _ArrayAdd($aChars,Chr($iChr))
    Next

    For $iDigits = 0 to 9
        _ArrayAdd($aChars,String($iDigits))
    Next
EndFunc


Func _IncrementString($sValueToIncrement)
    Local $aCharSplit[0]
    Local $aCharPtr[0]

    For $iSplitChars = 1 To StringLen($sValueToIncrement)
        _ArrayAdd($aCharSplit,StringMid($sValueToIncrement,$iSplitChars,1))
        For $iCheckCharArr = 0 To UBound($aChars)-1
            If $aCharSplit[UBound($aCharSplit)-1] == $aChars[$iCheckCharArr] Then
                _ArrayAdd($aCharPtr,$iCheckCharArr)
                ExitLoop
            EndIf
        Next
        If UBound($aCharSplit) <> UBound($aCharPtr) Then
            SetExtended($iSplitChars)
            Return False
        EndIf
    Next

    Local $bMax = True
    Local $sRet = ""

    For $iCheckMax = 0 to UBound($aCharSplit)-1
        If $aCharSplit[$iCheckMax] <> $aChars[UBound($aChars)-1] Then  ;Doing <> instead of Not = because of how autoit compares strings
            $bMax = False
            ExitLoop
        EndIf
    Next

    If $bMax Then
        For $iNewReturn = 0 to StringLen($sValueToIncrement)
            $sRet &= $aChars[0]
        Next
        Return $sRet
    EndIf

    For $iIncrementValues = UBound($aCharSplit)-1 to 0 Step -1
        If $iIncrementValues = UBound($aCharSplit)-1 Then $aCharPtr[$iIncrementValues] += 1
        If $aCharPtr[$iIncrementValues] > UBound($aChars) -1 Then
            $aCharPtr[$iIncrementValues] = 0
            $aCharPtr[$iIncrementValues-1] += 1
        Else
            ExitLoop
        EndIf

    Next

    For $iReformString = 0 To UBound($aCharSplit)-1
        $sRet &= $aChars[$aCharPtr[$iReformString]]
    Next
    Return $sRet

EndFunc

 

Link to comment
Share on other sites

2 hours ago, ali_lbay said:

i want the string go from AAAA to 9999 at the end.

Ahh, Ok, try this :

Local $sText = "AD9Q"

For $i = 0 To 160
  $sText = _IncrString ($sText)
  ConsoleWrite ($sText & @CRLF)
Next

Func _IncrString ($sString)

  Local $iLen = StringLen ($sString), $iChar, $sResult = ""
  For $i = $ilen To 1 Step -1
    $iChar = Asc(StringMid ($sString,$i,1))+1
    $iChar = ($iChar = 58 ? 65 : ($iChar = 91 ? 48 : $iChar))
    $sResult = Chr ($iChar) & $sResult
    If $iChar <> 65 Then Return StringLeft ($sString,$i-1) & $sResult
  Next
  Return $sResult

EndFunc

 

Link to comment
Share on other sites

A is zero! 🤔
 

For $i = 0 To 36^3 -1
    ConsoleWrite(CustomBaseConversion($i) & @LF)
Next

Func CustomBaseConversion($iInt, $iLength = 4)
    Local $iBase = 36, $iRemainder, $sOutput = ''

    Do
        $iRemainder = Mod($iInt, $iBase)
        $sOutput = Chr($iRemainder < 26 ? $iRemainder + 65 : $iRemainder + 22) & $sOutput
        $iInt = Floor($iInt/$iBase)
    Until $iInt < 1
    Return StringRight('AAAAAAAA' & $sOutput, $iLength)
EndFunc ;==> CustomBaseConversion

 

Link to comment
Share on other sites

A is zero. 

I am groot!

; Example, ABZ ==> AB0 , and, AB9 ==>ACA.
ConsoleWrite("ABZ" & " ==> " & _Dec2Base36(_Base36ToDec("ABZ") + 1, 3) & @CRLF) ; ABZ ==> AB0
ConsoleWrite("AB9" & " ==> " & _Dec2Base36(_Base36ToDec("AB9") + 1) & @CRLF) ;    AB9 ==> ACA

; From AAAA to 9999
For $i = 0 To _Base36ToDec("9999") ; 1,679,615
    ConsoleWrite(_Dec2Base36($i, 4) & @CRLF)
Next


; Modified from https://www.autoitscript.com/forum/topic/194949-array-troubles/?do=findComment&comment=1398101
; Parameter:
;    $iLen - 0 (default) the result will have 1 leading $aBase36[0] (representing zero, "0"), but in this case an "A".
;          - >0 The number of characters in result. eg. if $iLen = 4, the significant characters will be padded with
;               leading $aBase36[0]'s ("A"'s) to return the results character count to 4, or, stripped of leading characters.
;
Func _Dec2Base36($iDec, $iLen = 0)
    Local $aBase36 = StringSplit("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "", 2)
    Local $sRet = ""
    For $i = 0 To ($iLen ? $iLen - 1 : 20)
        $sRet = $aBase36[Mod($iDec / 36 ^ $i, 36)] & $sRet
    Next
    Return ($iLen ? $sRet : StringRegExpReplace($sRet, "^(" & $aBase36[0] & "+)", $aBase36[0]))
EndFunc   ;==>_Dec2Base36

Func _Base36ToDec($str)
    Local $iRet, $a = StringSplit($str, "", 2), $UB = UBound($a) - 1
    For $i = 0 To $UB
        $iRet += 36 ^ ($UB - $i) * (StringInStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", $a[$i], 1) - 1)
    Next
    Return $iRet
EndFunc   ;==>_Base36ToDec

 

Link to comment
Share on other sites

I add my 2 cents

  • function increments string and, if there is 'overflow', sets @extended
  • unexpected chars in input string are ignored and marked as '?' in output string
    Local $sMyString = "99U"
    
    ConsoleWrite($sMyString & @CRLF)  ; starting string
    For $i = 0 To 20
        $sMyString = _AlphaIncr1($sMyString)
        ConsoleWrite($sMyString & @TAB & (@extended ? "<- Overflow!" : "") & @CRLF)
    Next
    
    ; increment string and, if there is 'overflow', sets @extended
    ; unexpected chars in input string are ignored and marked as '?' in output string
    Func _AlphaIncr1($sInputString = '')
        Local Static $aDigits = StringSplit('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '', 2)
        Local $sResult = "", $Carry = $sInputString <> '', $IND = (0 / 0)
    
        For $i = StringLen($sInputString) To 1 Step -1
    
            $iIndex = Asc(StringMid($sInputString, $i, 1)) ; get one char at a time
            $iIndex -= $iIndex >= 65 ? $iIndex <= 90 ? 65 : $IND : $iIndex <= 57 ? $iIndex >= 48 ? 22 : $IND : $IND
    
            If String($iIndex) <> "-1.#IND" Then
                $iNextIndex = Mod($iIndex + $Carry, 36) ; 0 to 35
                $sNextChar = $aDigits[$iNextIndex]
                $Carry = $iNextIndex <> $iIndex + $Carry
            Else
                $sNextChar = "?" ; unexpected char in input
            EndIf
            $sResult = $sNextChar & $sResult
        Next
        Return SetError(0, $Carry, $sResult)
    EndFunc   ;==>_AlphaIncr1

     

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...