Search duplicates in delimited sting? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Search duplicates in delimited sting?

29 Jul 2019, 03:33

For example, I have the string

Code: Select all

red`nblue`ngreen`nred`nblack
The second "red" is a duplicate. Are there exiting functions in AHK to check it?
(Otherwise, an extra loop is not that hard ..)
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Search duplicates in delimited sting?  Topic is solved

29 Jul 2019, 03:52

you can use sort to remove duplicates. Or you can use instr twice, eg,

Code: Select all

f1(haystack, needle){	; finds duplicates without considering a delimiter
	local
	return (p := instr(haystack, needle)) && instr(haystack, needle,, p + 1)
}

f2(haystack, needle, del := "`n"){	; for when haystack is a delimited list
	local
	needle := del . needle . del
	haystack := del . haystack . del
	return (p := instr(haystack, needle)) && instr(haystack, needle,, p + 1)
}
You can also use regex, I leave that as an exercise ;).

Cheers.
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Search duplicates in delimited sting?

29 Jul 2019, 03:55

Thanks @Helgef :bravo:
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, OrangeCat and 172 guests