Search duplicates in delimited sting? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Peter2
Posts: 243
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 7 x64, AHK Version v1.1.22.xx
Helgef
Posts: 3927
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: 243
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 7 x64, AHK Version v1.1.22.xx

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], DuyMinh, Flipeador, flyingDman, gigiiiii, imnotgay, Sanbenito, x-tremespeed and 55 guests