removing duplicates from two separate strings

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

removing duplicates from two separate strings

21 Sep 2014, 20:12

The following code removes duplicates from a list to give me unique words:

Code: Select all

uniques =
listing = cat|dog|dog|fish|rabbit|cat

Loop, Parse, Listing, |
If InStr(listing,A_LoopField) = InStr(listing,A_LoopField,0,0)
uniques .= A_LoopField "|"
  
msgbox %uniques% 
But supposing I have two lists. What'S the most elegant way to remove words that occur in both lists and keep each list separate? This seems to work:

Code: Select all

listingA = cat|dog|fish|rabbit|
listingB = bee|cat|dog|gorilla|

uniquesA =
uniquesB =

loop, parse, listingA, |
{
	wrd := A_loopfield
	unique = Y
	loop, parse, listingB, |
	{
		if (wrd = A_loopfield)
		{
			unique = N
			break
		}
	}
	if unique = Y
	uniquesA .= A_LoopField "|"
}

loop, parse, listingB, |
{
	wrd := A_loopfield
	unique = Y
	loop, parse, listingA, |
	{
		if (wrd = A_loopfield)
		{
			unique = N
			break
		}
	}
	if unique = Y
	uniquesB .= A_LoopField "|"
}

msgbox %uniquesA%`n`n%uniquesB%
But is there a better way? Thanks.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: removing duplicates from two separate strings

21 Sep 2014, 21:57

instead of looping "listingA * listingB" twice, loop "listingA + listingB" twice like so

Code: Select all

listingA = ant|cat|dog|fish|rabbit|ant|
listingB = bee|cat|dog|gorilla|

uniquesA := uniquesB := ""
ObjA := [], ObjB := []

loop, parse, ListingA, |
	ObjA[A_LoopField] := ObjA[A_LoopField] ? ObjA[A_LoopField] + 1 : 1

loop, parse, ListingB, |
	ObjB[A_LoopField] := ObjB[A_LoopField] ? ObjB[A_LoopField] + 1 : 1

for k, v in ObjA
	if (v = 1) && !(ObjB[k])
		uniquesA .= k "|"

for k, v in ObjB
	if (v = 1) && !(ObjA[k])
		uniquesB .= k "|"

msgbox %uniquesA%`n`n%uniquesB%
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: removing duplicates from two separate strings

21 Sep 2014, 22:05

Code: Select all

listingA := "cat|dog|fish|rabbit"
listingB := "bee|cat|dog|gorilla"

uniquesA := Trim(RegExReplace(listingA, "(" listingB ")(\||$)"), "|")
removedA := Trim(RegExReplace(listingA, "(" uniquesA ")(\||$)"), "|")
uniquesB := Trim(RegExReplace(listingB, "(" removedA ")(\||$)"), "|")
MsgBox, % uniquesA "`n" uniquesB
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: removing duplicates from two separate strings

22 Sep 2014, 01:18

Thanks for the replies. Kon, I can't get your idea to work with a string generated from a variable. uniquesA is returning a list without any pipes. But the example you gave does work. What could the difference be?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: removing duplicates from two separate strings

22 Sep 2014, 01:55

The OP contains two different examples of "listing" variables. The second example has "|" as the rightmost character, the first does not. I chose to use a format similar to the first.
It's probably not working for you because the rightmost character of listingA or listingB is a pipe. Try using Trim on it.
Ronins
Posts: 85
Joined: 02 Oct 2013, 11:38

Re: removing duplicates from two separate strings

22 Sep 2014, 04:15

How about something like

Code: Select all

listingA := "cat|dog|fish|rabbit"
listingB := "bee|cat|dog|gorilla"
Uniques := listingA "|" listingB
Sort, Uniques, U D|
MsgBox, % Uniques
if you don't mind the sorted listing
Try out CMD class
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: removing duplicates from two separate strings

22 Sep 2014, 08:37

If you're on the latest version:

Code: Select all

listingA = cat|dog|fish|rabbit|
listingB = bee|cat|dog|gorilla|

For i, item in StrSplit(SubStr(listingA,1,StrLen(listingA)-1),"|")	; removes trailing pipe from string and splits into array
	if	!RegExMatch(listingB,"\b" item "\b")	; if the whole word/phrase is not in the list
		newlistingA .=	item "|"	; add to new list
MsgBox %	newlistingA
For i, item in StrSplit(SubStr(listingB,1,StrLen(listingB)-1),"|")	; removes trailing pipe from string and splits into array
	if	!RegExMatch(listingA,"\b" item "\b")	; if the whole word/phrase is not in the list
		newlistingB .=	item "|"	; add to new list
MsgBox %	newlistingB
return
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: removing duplicates from two separate strings

22 Sep 2014, 11:52

Code: Select all

listingA := "cat|dog|fish|rabbit"
listingB := "bee|cat|dog|gorilla"
listingC := "apple|cat|mango|gorilla|tank"
listingD := "apple|candle|maple"
listingE := "pet|dog"

MsgBox % strComp("|",listingA,listingB,listingC,listingD,listingE)

strComp(s,a,b*)	;s=separator a=stringa b=stringb or stringb,stringc,stringd,....
{							;returns only uniques
	for i, str in b
		b%i% := trim(str,s)
	l:=b.MaxIndex()+1,	b%l% := a
	loop % l
		all .= (all?s:"") b%A_index%
	Sort, all, U D%s%
	loop,parse, all, %s%
		if a_loopfield is not space
			{
			st := a_loopfield, cnt:=0
			loop % l
				if regexmatch(b%a_index%,"\b\Q" st "\E\b")
					cnt++
			if cnt=1
				u .= (u?s:"") st
			}
	return u
}
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: removing duplicates from two separate strings

22 Sep 2014, 20:11

Thanks for all the replies. A lot to mull over, especially the function by ahcahc.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1, MrDoge and 244 guests