Jump to content

Three letter increment


 Share

Recommended Posts

Hey guys, so I want to save a file with a random name where the last two letters of the filename are constants, ie 102618AA then 102618AB so on and so forth.. I used the code below to increment a single letter, but what about if I wanted to increment two or three letters, is there any way to do that? 

$c = Chr(Asc($current) + 1)

 

Link to comment
Share on other sites

You mean something like:

#include <Array.au3>

_Increment("AZ")
_Increment("AZY")

Func _Increment($_sString)
    Local $aString = StringSplit($_sString, "")
    For $i = 1 To $aString[0]
        If Asc($aString[$i]) = 90 Then
            $aString[$i] = Chr(65)
        Else
            $aString[$i] = Chr(Asc($aString[$i]) + 1)
        EndIf
    Next
    MsgBox(0,'', _ArrayToString($aString, "", 1))
EndFunc

 

Link to comment
Share on other sites

edit: i did the same thing @Subz did (a bit lazier), but I didnt spend those 5 minutes to not post it :)

edit2: since ours both reset on asc 90 (back to 65), you would have to ensure the string is uppercase

$sIn = StringUpper("ABCXYZ")
$sOut = ""

$arr = StringToASCIIArray($sIn)

For $i = 0 to ubound($arr) - 1
    $sOut &= $arr[$i] = 90 ? chr($arr[$i] + 1 - 26)  : chr($arr[$i] + 1)
Next

msgbox(0 , '' , $sOut)

 

Also, check my Gronsfeld Cipher that will let you increment based off an array of values rather than a static number 

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

To me this seems to be getting all the combo's 

Left for you to try ..

#include <Array.au3>

Local $aMain[0]
$str = ""
Local $hTimer = TimerInit()
For $i = 0 To 25
    $0 = 65 + $i
    $str0 = Chr($0)
    For $1 = $0 To 90
        $str1 = $str0 & Chr($1)
        For $2 = $0 To 90
            $str &= $str1 & Chr($2) & ";" & Chr($2) & $str1 & ";" & Chr($2) & StringReverse($str1) & ";"
        Next
    Next
Next
$a = _ArrayUnique(StringSplit($str, ";", 3))
_ArraySort($a)

$a[0] = "Time : " & TimerDiff($hTimer)
$a[1] = "Combo Count : " & $a[1] - 1
_ArrayDisplay($a)

Deye

Edited by Deye
Speed-Up Touch
Link to comment
Share on other sites

An easy way could be to use some brute force (as below) to first build your own array, store it in a txt file, then use FileReadToArray when needed and increment using the array index

$Form1 = GUICreate("Form1", 150, 310, 200, 100)
$List = GUICtrlCreateList("", 20, 20, 110, 250)
$label = GUICtrlCreateLabel("Loading ...", 20, 280, 110, 20)
GUISetState()

$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Global $text = StringSplit($str, "", 3), $n = 0, $long = 3  
_Go(0,"")
GUICtrlSetData($label, $n & "  solutions")

While GUIGetMsg()<>-3
Wend

Func _Go($temp, $string)
    For $i = 0 to UBound($text)-1
        If $temp = $long-1 Then
             GUICtrlSetData($List, $string & $text[$i] & "|")
             $n += 1
        EndIf
        If $temp < $long-1 Then
           _Go($temp+1, $string & $text[$i] )
        EndIf
    Next
EndFunc

 

Link to comment
Share on other sites

Updated My example, Since I see i was missing quite a few thousand combos ..

Edit:

"I was thinking of a function that would increment like AAA...AAB...AAC...AAD....{more increments}....AAZ..ABA.."

 @ following what you describe 1 to 1 , here is my take ..

#include <Array.au3>

$sfile = "A"

While 1
    $str = ""
    For $1 = 1 To 26
        $sfile = Increment($sfile)
        $str &= $sfile & ";"
    Next
    ConsoleWrite($str & @CRLF)
    If StringLen($sfile) > 3 Then ExitLoop
WEnd

Func Increment($sfile)
    If Stringlen <= 2 Then
        If StringLeft($sfile, 1) = "Z" Then
            Return "A" & StringTrimLeft($sfile, 1) & "A"
        EndIf
    EndIf
    Local $arr = StringSplit($sfile, "")
    For $i = UBound($arr) - 1 To 0 Step -1
        If Asc(StringRight($arr[$i], 1)) - 89 = 1 Then ContinueLoop
        $arr[$i] = Chr(Asc($arr[$i]) + 1)
        If $i < UBound($arr) - 1 Then
            $arr[$i + 1] = "A"
        EndIf
        Return _ArrayToString($arr, "", 1)
    Next
;~  Return $sfile & "A"
EndFunc   ;==>Increment

 

Edited by Deye
Link to comment
Share on other sites

This method converts an alphabetic number to its decimal equivalent and adds one.  Then coverts that decimal number to its alphabetic number equivalent. 

#include <Array.au3>
; Modified functions from https://www.autoitscript.com/forum/topic/194949-array-troubles/?do=findComment&comment=1398101

Local $sNum = "AZZ"
MsgBox(0, "", $sNum & " + 1 = " & _Dec2Base26(_Base26ToDec($sNum) + 1) & @CRLF)

; Parameter $iLen - Number of characters in returned string.
Func _Dec2Base26($iDec, $iLen = 3)
    Local $aBase26 = StringSplit("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "", 2)
    Local $sRet = ""
    For $i = 0 To Int(Log($iDec) / Log(26))
        $sRet = $aBase26[Mod($iDec / 26 ^ $i, 26)] & $sRet
    Next
    Return StringRight("AAAAAAAAAAAA" & $sRet, $iLen)
EndFunc   ;==>_Dec2Base26

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

 

Link to comment
Share on other sites

Discarding the UnDebugged "If Stringlen <= 2 Then"   form my previous post ( left it as unedited for reviewers ..)

For other cases that head up to my attention from Malkey's post above, Here is an Update that conducts the suitable approach:

#include <Array.au3>

MsgBox(0, '', Inc("AYZ"))

Func Inc($sStr)
    Local $sSl = StringTrimRight($sStr, 1)
    If StringRight($sStr, 1) <> "Z" Then Return $sSl & Chr(Asc(StringRight($sStr, 1)) + 1)
    Local $sSr = "A"
    While StringRight($sSl, 1) = "Z"
        $sSr &= "A"
        $sSl = StringTrimRight($sSl, 1)
    WEnd
    If $sSl = "" Then Return $sSr & "A"
    $sSr = Chr(Asc(StringRight($sSl, 1)) + 1) & $sSr
    Return StringTrimRight($sSl, 1) & $sSr
EndFunc   ;==>Inc

Deye

Edited by Deye
Link to comment
Share on other sites

i think this also meets the requirement for 3 letter increments... i have been wronger before tho

#include<array.au3>

$aValues = stringsplit("ABCDEFGHIJKLMNOPQRSTUVWXYZ" , '' , 2)

$str = "XYZ"
;~ $str = "XZZ"


$aStr = StringSplit(StringReverse($str) , "" ,2)

for $i = 0 to ubound($aStr) - 1
    If $aStr[$i] = "Z" Then
        $aStr[$i] = "A"
    Else
        $aStr[$i] = $aValues[_ArraySearch($aValues , $aStr[$i]) + 1]
        ExitLoop
    EndIf
Next

msgbox(0, '' , StringReverse(_ArrayToString($aStr , "")))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

....a compact way, it also flags you the overflow.
The error flag is setted to 1 if the returned string is in overflow, that is when "ZZZZ" became AAAA.

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

 

 

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...