Assign string a variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kurza
Posts: 29
Joined: 23 Apr 2016, 02:01
Location: Granada, Spain

Assign string a variable

23 Jan 2018, 14:05

No code I'm afraid. What I am trying to do is covered by several document explanations and I cant get it.
I have the haystack (a simple .txt file). I input the needle (which is a product code) into a guí. The next 30 characters after the found needle is the product description. I need to assign the 30 characters a variable which will be returned to the gui (which I can do.) I can't work out how to assign the 30 characters after the needle a variable.

Of course any suggestions will be dealt with with love.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Assign string a variable

23 Jan 2018, 14:21

You input the product code and expect to get the product description in return, right?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Assign string a variable

23 Jan 2018, 14:32

Hi,

Here's a way to achieve this using SubStr, InStr and StrLen functions.

Code: Select all

haystack := "AutoHotkey aka AHK. description: Automation scripting language."
needle := "description: "
needleLength := StrLen(needle)
position := InStr(haystack, "description: ")
cuantosCaracteres := 10
substring := SubStr(haystack, position+needleLength, cuantosCaracteres)
MsgBox % substring
substring := SubStr(haystack, InStr(haystack, "description: ")+StrLen(needle), cuantosCaracteres)
MsgBox % substring
my scripts
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Assign string a variable

23 Jan 2018, 19:06

A_AhkUser wrote:Hi,

Here's a way to achieve this using SubStr, InStr and StrLen functions.

Code: Select all

haystack := "AutoHotkey aka AHK. description: Automation scripting language."
needle := "description: "
needleLength := StrLen(needle)
position := InStr(haystack, "description: ")
cuantosCaracteres := 10
substring := SubStr(haystack, position+needleLength, cuantosCaracteres)
MsgBox % substring
substring := SubStr(haystack, InStr(haystack, "description: ")+StrLen(needle), cuantosCaracteres)
MsgBox % substring
Little simplier with RegEx:

Code: Select all

haystack := "AutoHotkey aka AHK. description: Automation scripting language."
needle := "description: "
cuantosCaracteres := 10
RegExMatch(haystack, needle "(.{" cuantosCaracteres "})", Match)
MsgBox % Match1
Example closer to OP:

Code: Select all

haystack := "Product ID 56412-3889 a string less than 30"
needle := "56412-3889"
RegExMatch(haystack, needle "(.{1,30})", Match)
MsgBox % Match1

haystack := "Product ID 56412-3889 a simple string that is 30.  And then stuff you don't want"
needle := "56412-3889"
RegExMatch(haystack, needle "(.{1,30})", Match)
MsgBox % Match1
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
kurza
Posts: 29
Joined: 23 Apr 2016, 02:01
Location: Granada, Spain

Re: Assign string a variable

10 Mar 2018, 05:20

Thanks for replying. I accidentally deleted my GUI. Well to save time I used it as a template for another GUI and pressed save instead of saving it under another name so I'll have to sit one day and recreated it. I did check out the codes above for functionality and I was able to get the shorter of the codes to give back some anticipated responses.

Thanks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Ineedhelplz, jaka1, penguinautomator, Spawnova and 279 guests