Dgameman1 Posted May 2, 2016 Share Posted May 2, 2016 I know I can use _ArrayUnique to give back the Unique arrays, but I want to actually delete both of the duplicates if there's more than one at all. Local $Usernames[3] = ["Dan", "Bob", "Billy"] Local $blackListUsernames[2] = ["Dan", "Scott"] _ArrayAdd($Usernames, $blackListUsernames) ;combines the 2 arrays into 1. $Usernames = _ArrayUnique($Usernames) ;Uniques the array. This issue with this, is that $Usernames will contain Dan, Bob, Billy, Scott. I can't seem to find a way to make it so that if there are any duplicates, it'll just remove both of them, for example, I want $Usernames to have just Bob, Billy, Scott. There will only be either two or one of the same value. Never more than 2. Link to comment Share on other sites More sharing options...
AutoBert Posted May 2, 2016 Share Posted May 2, 2016 (edited) Do it like this: #include <Array.au3> Local $Usernames[3] = ["Dan", "Bob", "Billy"] Local $blackListUsernames[2] = ["Dan", "Scott"] For $i=UBound($Usernames)-1 To 0 Step -1 $iFound=_ArraySearch($blackListUsernames,$Usernames[$i]) If Not @error Then _ArrayDelete($Usernames,$i) _ArrayDelete($blackListUsernames,$iFound) EndIf Next _ArrayDisplay($Usernames) _ArrayDisplay($blackListUsernames) _ArrayAdd($Usernames, $blackListUsernames) ;combines the 2 arrays into 1. _ArrayDisplay($Usernames) For arrays generated from web etc. both arrays have to be unique before using the For..Next loop: #include <Array.au3> Local $Usernames[4] = ["Dan", "Bob", "Billy","Bob"] $Usernames = _ArrayUnique($Usernames) ;Uniques the array. _ArrayDelete($Usernames,0) Local $blackListUsernames[3] = ["Dan", "Scott", "Dan"] $blackListUsernames = _ArrayUnique($blackListUsernames) ;Uniques the array. _ArrayDelete($blackListUsernames,0) For $i=UBound($Usernames)-1 To 0 Step -1 $iFound=_ArraySearch($blackListUsernames,$Usernames[$i]) If Not @error Then _ArrayDelete($Usernames,$i) _ArrayDelete($blackListUsernames,$iFound) EndIf Next _ArrayDisplay($Usernames) _ArrayDisplay($blackListUsernames) _ArrayAdd($Usernames, $blackListUsernames) ;combines the 2 arrays into 1. _ArrayDisplay($Usernames) Edited May 2, 2016 by AutoBert Synapsee and Dgameman1 2 Link to comment Share on other sites More sharing options...
mikell Posted May 2, 2016 Share Posted May 2, 2016 (edited) A funny and fast way using Scripting.Dictionary #include <Array.au3> Local $a[4] = ["Dan", "Bob", "Billy", "Bob"] Local $b[3] = ["Dan", "Scott", "Dan"] ; create the dictionaries Local $sda = ObjCreate("Scripting.Dictionary") Local $sdb = ObjCreate("Scripting.Dictionary") ; populate them For $i In $a $sda.Item($i) Next For $i In $b $sdb.Item($i) Next ; list the wanted items Local $sdc = ObjCreate("Scripting.Dictionary") For $i In $a If not $sdb.Exists($i) Then $sdc.Item($i) Next For $i In $b If not $sda.Exists($i) Then $sdc.Item($i) Next ; get the result array $aResult = $sdc.Keys() _ArrayDisplay($aResult, "$aResult") Edited May 2, 2016 by mikell AutoBert 1 Link to comment Share on other sites More sharing options...
jchd Posted May 2, 2016 Share Posted May 2, 2016 (edited) It doesn't work when, for instance, $b is ["Dan", "Scott", "Dan"] because "Dan" remains in the final array. Edited May 2, 2016 by jchd Was fixed in the meantime This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jguinch Posted May 2, 2016 Share Posted May 2, 2016 (edited) Another one, based on Scripting.Dictionary : #Include <Array.au3> Local $a = ["Dan", "Bob", "Billy", "Dan", "Scott"] Local $aUniqs = _ArrayRemoveDuplicates($a) _ArrayDisplay($aUniqs) Func _ArrayRemoveDuplicates($aArray) Local $oDictionary = ObjCreate("Scripting.Dictionary") For $i = 0 To UBound($aArray) - 1 $oDictionary.Item($aArray[$i]) += 1 Next For $vVal In $oDictionary.Keys() If $oDictionary.Item($vVal) > 1 Then $oDictionary.Remove($vVal) EndIf Next Return $oDictionary.Keys() EndFunc Edited May 2, 2016 by jguinch modification to use $oDictionary.Remove Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted May 2, 2016 Share Posted May 2, 2016 @jchd I saw this and edited immediately Link to comment Share on other sites More sharing options...
mikell Posted May 2, 2016 Share Posted May 2, 2016 (edited) @jguinch <snip> Your comment below is right, it's definitely not my day Edited May 2, 2016 by mikell Synapsee 1 Link to comment Share on other sites More sharing options...
jguinch Posted May 2, 2016 Share Posted May 2, 2016 No Mikell, that's not the same result. Look at what the OP asked for : I want $Usernames to have just Bob, Billy, Scott. Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
iamtheky Posted May 2, 2016 Share Posted May 2, 2016 (edited) #Include <Array.au3> Local $a = ["Dan", "Bob", "Billy", "Dan", "Scott" , "Dan" , "Bob" , "Bob" , "Dan"] _ArraySort($a) _ArrayDisplay(stringsplit(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(_ArrayToString($a , ",") , "(\w+?),\g1" , "$1") , "(\w+?),\g1" , "") , ",+" , ",") , "," , 2)) *And Btw, if you dont fix my arraydisplay with code, and instead just reply: "but it only works for that array AND looks ridiculous." Then you are pointing out many obvious things, none of which get us any closer to a working one line display from a sorted array. Edited May 2, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted May 2, 2016 Share Posted May 2, 2016 This one is.......less wrong, but still operates by sheer luck. #Include <Array.au3> Local $a = ["Dan", "Bob", "Billy", "Dan", "Scott" , "Dan" , "Bob" , "Dan"] _ArraySort($a) msgbox(0, '' , StringRegExpReplace(StringRegExpReplace (_ArrayToString($a , ",") , "(\b\w+\b)\W\1" , "") , ",+" , ",")) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted May 2, 2016 Share Posted May 2, 2016 iamtheky, The last one doesn't work if the number of duplicates is odd Try this one : Msgbox(0,"", StringRegExpReplace(_ArrayToString($a, ","), '(\b\w+\b),(\1,?)+', "") ) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now