I have a loop for an array, but what if the data isn't an array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Murica
Posts: 29
Joined: 27 Nov 2022, 17:28

I have a loop for an array, but what if the data isn't an array?

Post by Murica » 27 Nov 2022, 17:36

What's a better way to do this?

Code: Select all

; IF ARRAY
if (inStr(data,","))
   {data:=strsplit(data, ",")
   for i, var in data
      do_stuff(var)
   }
else ; IF STRING
   do_stuff(data)

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: I have a loop for an array, but what if the data isn't an array?

Post by boiler » 27 Nov 2022, 18:07

No Need for the if statement and the else branch. Assuming you want to call the function with each piece separated by commas in the initial string (even if there is only one piece with no commas), just do this:

Code: Select all

data := StrSplit(data, ",")
for i, var in data
	do_stuff(var)

Alternatively, use a parsing loop:

Code: Select all

loop, Parse, data, CSV
	do_stuff(A_LoopField)

Post Reply

Return to “Ask for Help (v1)”