Number Zero lost after Split&adding numbers Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Number Zero lost after Split&adding numbers

18 Sep 2017, 08:20

Code: Select all

Clipboard=
ClipWait
myvar:=Clipboard ;FN0089
if( RegExMatch(myvar, "(.*?)(\d+)$", splitted) )
splitted2 := splitted2 +1
Sleep 100
MsgBox, %splitted1%%splitted2% ;FN90 (supposed to be FN0090)
i'm sorry it's some kind of stupid issue, but i'm still looking around how i can fix it...
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Number Zero lost after Split&adding numbers

18 Sep 2017, 08:28

[url=https://autohotkey.com/boards/viewtopic.php?t=13303]Numbers as strings, how to force treat as string{/url]
i have no idea why the zero set to be trimmed by default in autohotkey? i'm trying to find a simple way to keep the zero after split&add functions
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Number Zero lost after Split&adding numbers

18 Sep 2017, 08:33

Code: Select all

ClipBoard := "FN0089"			; for testing
SetFormat, float, 04.0			; "reset" zero padding
myVar := SubStr(Clipboard,3)	; extract the number from the (fixed lenght) string
MsgBox % "FN" . myvar += 1.0	; add 1 to the current number and concatinate it with the string
A more sophisticated method: :arrow: Format ...
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Number Zero lost after Split&adding numbers

18 Sep 2017, 08:53

BoBo wrote:

Code: Select all

ClipBoard := "FN0089"			; for testing
SetFormat, float, 04.0			; "reset" zero padding
myVar := SubStr(Clipboard,3)	; extract the number from the (fixed lenght) string
MsgBox % "FN" . myvar += 1.0	; add 1 to the current number and concatinate it with the string
A more sophisticated method: :arrow: Format ...
reliable answer, thank you very much
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Number Zero lost after Split&adding numbers  Topic is solved

18 Sep 2017, 09:07

Hi ivill,

There are often many ways to achieve objectives in AHK. Another way to do it is shown here:
https://autohotkey.com/boards/viewtopic ... 09#p170209

And explained here:
https://autohotkey.com/boards/viewtopic ... 34#p170634

So, in your case, you can leave all of your code as is, except change the splitted2 assignment statement to:

Code: Select all

splitted2:=SubStr("0000" . splitted2+1,-3)
Regards, Joe
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Number Zero lost after Split&adding numbers

18 Sep 2017, 09:13

See format. Eg splitted2 := format("{:04}", splitted2 + 1)
Edit: I see now that Bobo mentioned it, my eyes are confused by all that green / yellow stuff. :crazy:
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Number Zero lost after Split&adding numbers

20 Sep 2017, 10:20

JoeWinograd wrote:Hi ivill,

There are often many ways to achieve objectives in AHK. Another way to do it is shown here:
https://autohotkey.com/boards/viewtopic ... 09#p170209

And explained here:
https://autohotkey.com/boards/viewtopic ... 34#p170634

So, in your case, you can leave all of your code as is, except change the splitted2 assignment statement to:

Code: Select all

splitted2:=SubStr("0000" . splitted2+1,-3)
Regards, Joe
Thanks Joe, your code works handy.
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: Number Zero lost after Split&adding numbers

20 Sep 2017, 10:22

:think:
Helgef wrote:See format. Eg splitted2 := format("{:04}", splitted2 + 1)
Edit: I see now that Bobo mentioned it, my eyes are confused by all that green / yellow stuff. :crazy:
Thanks for all of you, i took up Joe's suggestion,

Code: Select all

splitted2:=SubStr("0000" . splitted2+1,-3)
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Number Zero lost after Split&adding numbers

20 Sep 2017, 12:07

Hi ivill,
You're welcome — I'm glad that works for you. Regards, Joe
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Number Zero lost after Split&adding numbers

20 Sep 2017, 12:51

In AHK v2, RegExMatch will output objects, and not variables, also SubStr will handle negative offsets differently. For these reasons I would suggest this more forwards compatible solution:

Code: Select all

q::
vText := "FN0089"
vText := RegExReplace(vText, "\d+$", "|$0")
oTemp := StrSplit(vText, "|")
vText := oTemp.1 Format("{:04}", oTemp.2+1)
oTemp := ""
MsgBox, % vText
return
Or this one:

Code: Select all

q::
vText := "FN0089"
vText := RegExMatch(vText, "O)(.*?)(\d+)$", o)
;vText := o.Value(1) Format("{:04}", o.Value(2)+1)
vText := o.1 Format("{:04}", o.2+1)
o := ""
MsgBox, % vText
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], bobstoner289, Ralf_Reddings200244 and 298 guests