Jump to content
Sign in to follow this  
y4m4

Leading Zeros[Resolved]

Recommended Posts

y4m4

I am making a script that splits a string into two parts, numeric and non-numeric. Then it Sends the part of the non-numeric string. Next I add 1 to the numeric portion and then Send that.

Here is the code:

dim $gname
dim $gnameT
dim $gnum

func gamecop()
    send("^c")
    sleep(100)
    $gnameT = ClipGet()
    $gnum = StringLower($gnameT)
    $gname = StringLower($gnameT)
$charas = _ArrayCreate("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " ", "-")
$numas = _ArrayCreate("1", "2", "3", "4", "5", "6", "7", "8", "9", "0")

for $o = 0 to UBound($charas) - 1
    $gnum = StringReplace($gnum, $charas[$o], "")
Next

MsgBox(0, "", $gnum)
for $p = 0 to UBound($numas) - 1
    $gname = StringReplace($gname, $numas[$p], "")
Next
MsgBox(0, "", $gname)
EndFunc

func gamepas()
    send($gname)
    if StringRegExp($gnum,
    $gnum = 1 + $gnum
    send($gnum)
    sleep(100)
    send("{RSHIFT}")
EndFunc

Yes, I edited the Array.au3 to allow using _ArrayCreate for arrays up to size 30.

So the issue I have is that the numeric portion always gets the leading zero lopped off.

How can I stop this without using complicated If statements to ensure that the 0 remains unless it is followed by a 9 which will be increased to a 10 which would change that leading zero into a 1(if that makes any sense).

Thanks.

Oh yeah, I've tried using functions like String, Number and Int to keep any leading zeros intact.

Edited by y4m4

Share this post


Link to post
Share on other sites
zorphnog

StringRegExp and StringRegExpReplace would make your life a whole lot easier, but before we go into that how about some example input data? Show us what data you are inputing, the expected result, and the actual result you are receiving.

Share this post


Link to post
Share on other sites
y4m4

I was looking at StringRegExp but I don't know how to make it do what I want.

So sample data... hmmm

trial-010

What I want to get is this

trial-011

What I get is this

trial-11

I'd select that text and activate gamecop() It splits the string up and returns each part in a message box; this is for testing and it works fine. Then I'd activate gamepas() and it Sends the non-numeric portion of the string, adds 1 to the number and then Sends the number except it always leaves off the leading zero.

Share this post


Link to post
Share on other sites
zorphnog

Well if the format of the name is always "[game name]-[three digit number]" then I would do something like this:

$sInGame = "trial-009"

gamepas(gamecop($sInGame))

Func gamecop($sGame)
    Local $aSplit
    $aSplit = StringRegExp($sGame, "(?i)([a-z|\s]+\-)([0-9]+)", 3)
    Switch @error
        Case 1
            ConsoleWrite("!> Error in StringRegExp: No matches" & @LF)
            Exit
        Case 2
            ConsoleWrite("!> Error in StringRegExp: Bad pattern" & @LF)
            Exit
    EndSwitch
    ConsoleWrite("+> Game: " & $aSplit[0] & @TAB & "Number: " & $aSplit[1] & @LF)
    Return $aSplit
EndFunc

Func gamepas($aGame)
    Send($aGame[0])
    Send(StringFormat("%03d", Int($aGame[1])+1))
    Sleep(100)
    Send("{RSHIFT}")
EndFunc

Mainly just look at the StringFormat call to see how to format leading zeros.

Edited by zorphnog

Share this post


Link to post
Share on other sites
y4m4

Thanks!

Here's what I did that seems to have fixed my problem:

func gamecop()
    send("^c")
    sleep(100)
    $gnameT = ClipGet()
    $gnum = StringLower($gnameT)
    $gname = StringLower($gnameT)
$charas = _ArrayCreate("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " ", "-")
$numas = _ArrayCreate("1", "2", "3", "4", "5", "6", "7", "8", "9", "0")

for $o = 0 to UBound($charas) - 1
    $gnum = StringReplace($gnum, $charas[$o], "")
Next
$gnumLen = StringLen($gnum)
MsgBox(0, "", $gnum)
for $p = 0 to UBound($numas) - 1
    $gname = StringReplace($gname, $numas[$p], "")
Next
MsgBox(0, "", $gname)
EndFunc

func gamepas()
    
    
    send($gname)
    
    $gnum = 1 + $gnum
    
    send(StringFormat("%0" & $gnumLen & "d", Int($gnum)))
    sleep(100)
    send("{RSHIFT}")
EndFuncƒoÝŠ÷ ØZ.®ØeŠw®¦+-…ç!jxjëhŠ×6Send(StringFormat("%0" & $gnumLen & "d", Int($gnum)))
I get the length in gamecop()

I get the length of the number and use it in the formatting, works like a charm.

This way I can have any number of leading zeros! StringFormat is exactly what I needed and your example made its usage clear(enough ;) ).

I would have used your split method except the formatting of the game name changes depending on who is making it. So it could be anything like these:

trial001
trial1
trial-19
trial-088

And so on.

Thanks again =)

Edited by y4m4

Share this post


Link to post
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
Sign in to follow this  

×