Limit the Number of Substrings Delivered by the StrSplit() Function (AutoHotkey Quick Tip)

A New Parameter in the AutoHotkey StrSplit() Function Makes Selective Parsing Possible

robotparse.pngAt the same time as when we saw the release of the new Hotstring() function and other added Hotstring features (AutoHotkey version 1.1.28.00 – February 11, 2018), the powers-that-be made a significant change to the StrSplit() function. I don’t know if the idea grew from the need to parse Hotstrings or just happened to coincide with that release of AutoHotkey.

I discovered the new power when I ran into trouble working with the InstantHotstring.ahk script while using a Web address as replacement text. This useful new StrSplit() parameter (MaxParts) might save you from adding more code when you need to limit the number of produced pieces of text. (Don’t believe it when they say, “Parts is parts!”)

Truncated URLs

Beginning AutoHotkey Hotstrings 200pxIn the InstantHotstring.ahk script, the setting, viewing, loading, and toggling the activation of Hotstrings requires the parsing of the Hotstring statement code into options, activating string, and replacement text for use in the Hotstring() function and/or the script’s GUI controls. The colon ( : ) acts as the most obvious delimiting character since all critical data rests between the various colon marks. The StrSplit() function seemed to work well, however, I noticed a problem when I added a Web URL as the Hotstring replacement text.

After the initial activation of the Hotstring and when viewing or reloading Hotstrings from a file, the script dropped everything in the replacement text appearing after the initial “https:” protocol prefix.

instanthotstring http

I immediately understood the problem. Since I used the colon as the delimiter to parse the Hotstring:

HotString := StrSplit(Select, ":")

everything after the “https:” colon jumped into the next array variable HotString[6]. If more colons appeared in the text, AutoHotkey would create even more array variables. I could repair the damage with a simple loop, but I decided to take another look at the StrSplit() function seeking a method for limiting the number of text string variables.

Surprisingly, I discovered that the StrSplit() function now supports exactly that type of parsing limitation. (I was even more surprised to see that the new parameter appeared on the scene in the same release as the Hotstring() function.) By adding the new parameter (MaxParts) to the StrSplit() function AutoHotkey limits the number of pieces returned in the array:

HotString := StrSplit(Select, ":", , 5)

The leftovers get thrown into the last array variable—exactly what I wanted. I added the new parameter to the appropriate StrSplit() functions in the InstantHotstring.ahk script.

When using the MaxParts parameter, the StrSplit() function processes one less delimiter than the assigned number—in this case, four delimiting colons to create five (5) text segment array variables. The function ignores any additional delimiting characters embedded in the string (in the InstantHotstring.ahk script, colons)—no matter how many.

Use this MaxParts parameter anytime you need to parse data from the beginning of a string while leaving the remainder of the string intact in the last array variable—even if it contains more of that same delimiting character.

Note: The optional third parameter in the StrSplit() function (OmitChars), not shown above, tells AutoHotkey which characters to remove from the beginning and end (not the middle) of any parsed substring. This produces an array variable result similar to the Trim() function which removes spaces and/or tabs from the beginning and end of a string.

For next time, before I return to loading Hotstring files into the InstantHotstring.ahk script, I have another quick tip which impacts Hotstrings using a Web URL as replacement text.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

 

One thought on “Limit the Number of Substrings Delivered by the StrSplit() Function (AutoHotkey Quick Tip)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s