Arrays and string manipulation optimization Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BlooDoesntPlay
Posts: 17
Joined: 26 Nov 2021, 07:39

Arrays and string manipulation optimization

Post by BlooDoesntPlay » 02 Dec 2021, 03:16

Hello again AutoHotKey forums! I've returned once again, because I'm positive I'm going about this the wrong way and I'd appreciate if someone could point me in the right direction on how I should be writing this out. I'm having issues using a for with an array, I'm also having to use temporary variables to store things because I can't use .MaxIndex() or SubStr() inside other things, and I don't know if I'm going about it the right way.

The code snippet I'll provide is part of something much larger that works, but is pretty self contained. I really only need to understand what I'm doing ass backwards here. Programming in C++, I've made some really gnarly string manipulation programs that took crazy text files and cut them into readable formats for programs, but I can barely find and or use the string manipulation functions here without tripping up in some which way.

This snippet will put up an input box to ask you for names. For this example, I'll put in "Alex Brandon Casey Dave", it'll add a space on the end because I'm too lazy to add a special case where it reaches the end and must grab the rest of the string, and it'll keep finding spaces and add the previous string until there are no more spaces left, adding them all into an array. It will then read the array back to me one at a time and then say how big the array is. I know that's pretty useless, just reading it back, it's the easiest way to see that I'm correctly turning a long string full of spaces into an array that divides it all up. Please tell me how I can optimize it. I've considered also making input boxes until you no longer input something, and you can insert them all into an array that way so they are divided up way easier but I don't like that, I wanna play with strings.

Code: Select all

global NameList := Object()
^9::
	NameList := Object()
	InputBox, names, Users, Enter names with spaces inbetween, , 500, 300
	names := names . " "
	
	loop
	{
		ToFind := " "
		StringGetPos, pos, names, %ToFind%
		if (pos = -1)
		{
			ToolTip done
			break
		}
		TempString := SubStr(names,1,pos)
		NameList.Insert(TempString)
		names := SubStr(names,pos+2)
	}
	
	index := 1
	while index <= NameList.MaxIndex()
	{
		TempSave := NameList[index]
		ToolTip %TempSave%
		Sleep 500
		index += 1
	}
	Maxs := NameList.MaxIndex()
	ToolTip %Maxs%
	Sleep 500
return
Thanks in advance!

Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Arrays and string manipulation optimization  Topic is solved

Post by Rohwedder » 02 Dec 2021, 03:57

Hallo,
try:

Code: Select all

global NameList := Object()
^9::
InputBox, names, Users, Enter names with spaces inbetween, , 500, 300
For all, TempSave in StrSplit(RegExReplace(Trim(names), "\h+", A_Space), A_Space)
{	;Removes Spaces or Tabs at the ends,
	;replaces multiple Spaces and Tabs with one Space
	;and splits at each of these Spaces
	NameList.Push(TempSave)
	ToolTip %TempSave%
	Sleep 500
}
Maxs := NameList.MaxIndex()
ToolTip %Maxs%
Sleep 500
return

BlooDoesntPlay
Posts: 17
Joined: 26 Nov 2021, 07:39

Re: Arrays and string manipulation optimization

Post by BlooDoesntPlay » 02 Dec 2021, 05:20

Rohwedder wrote: Hallo,
try:

Code: Select all

global NameList := Object()
^9::
InputBox, names, Users, Enter names with spaces inbetween, , 500, 300
For all, TempSave in StrSplit(RegExReplace(Trim(names), "\h+", A_Space), A_Space)
{	;Removes Spaces or Tabs at the ends,
	;replaces multiple Spaces and Tabs with one Space
	;and splits at each of these Spaces
	NameList.Push(TempSave)
	ToolTip %TempSave%
	Sleep 500
}
Maxs := NameList.MaxIndex()
ToolTip %Maxs%
Sleep 500
return
What a great example! Might I ask, what page would you find the information for the pattern on \h+ ? It looks to be a space or tab until not space or tab pattern, but I can't find anything on h anywhere on regular expressions. Anyways, I love your example, and thanks.

Rohwedder
Posts: 7553
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Arrays and string manipulation optimization

Post by Rohwedder » 02 Dec 2021, 06:06

http://www.pcre.org/pcre.txt
Generic character types

Another use of backslash is for specifying generic character types:

\d any decimal digit
\D any character that is not a decimal digit
\h any horizontal white space character
\H any character that is not a horizontal white space character
\s any white space character
\S any character that is not a white space character
\v any vertical white space character
\V any character that is not a vertical white space character
\w any "word" character
\W any "non-word" character

Post Reply

Return to “Ask for Help (v1)”