Page 1 of 1

Search duplicates in delimited sting?

Posted: 29 Jul 2019, 03:33
by Peter2
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 ..)

Re: Search duplicates in delimited sting?  Topic is solved

Posted: 29 Jul 2019, 03:52
by Helgef
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.

Re: Search duplicates in delimited sting?

Posted: 29 Jul 2019, 03:55
by Peter2
Thanks @Helgef :bravo: