Jump to content

UNIQUE ELEMENTS IN ARRAY - (Moved)


VinMe
 Share

Recommended Posts

HELLO All,

is there any function in Auto it to Remove the repeating  strings separated by ","  which is present in the array.

ex.

Available state

a[0]= D97,D96,,D85,D86,D85,D86,D85,D86,D85,,D86,D85,D86,D85,D86

a[1]=D85,D24,,,,,D85

Required state

a[0]= D97,D96,D85,D86,

a[1]=D85,D24

thank  you in advance!

vin!

Link to comment
Share on other sites

#include <StringConstants.au3>
#include <Array.au3>
Global $g_sString, $g_sOutput
$g_sString = "D97,D96,,D85,D86,D85,D86,D85,D86,D85,,D86,D85,D86,D85,D86"

; remove duplicates :
$g_sOutput = _ArrayToString(_ArrayUnique(StringSplit($g_sString, ",", $STR_NOCOUNT), 0, 0, 0, $ARRAYUNIQUE_NOCOUNT), ",")
ConsoleWrite('> >>> String Output = ' & $g_sOutput & @CRLF)

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Another way:

Local $sString = "D97,D96,,D85,D86,D85,D86,D85,D86,D85,,D86,D98,D85,D86,D85,D86"

; remove duplicates :
Local $sOutput = StringRegExpReplace(StringRegExpReplace($sString, "(\b\w+\b,?)(?=.*?\1)", ""), ",+", ",")
ConsoleWrite($sString & @LF & $sOutput & @LF)

 

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 here
RegExp tutorial: enough to get started
PCRE 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

Another example.

#include <Debug.au3>; Included For _DebugArrayDisplay() use only.
; https://www.autoitscript.com/forum/topic/201783-unique-elements-in-array-moved/?do=findComment&comment=1447861

Local $a[3] = [ _
        "D97,,,D96,D85,D85,D86,,D8!6,D85,D8!6,,CD85,,D86,D98,,,,,D85,D86,CD85,,D86", _
        ",,D85,D24,,,,,D85", _
        "D85,D24,,,,,,D85,,"]

For $i = 0 To UBound($a) - 1
    ; Remove duplicate elements; Remove leading and trailing commas; Remove all the commas followed by a comma (one comma remains from a whole group of commas).
    ;$a[$i] = StringRegExpReplace($a[$i], "(\b\w+\b),*(?=.*?\1)|(^,+|,+$)|,+(?=,)", "")

    ; Overcomes "\b" problem by matching non-word characters embedded in the comma separated "test" string.
    ;$a[$i] = StringRegExpReplace($a[$i], "((?<=,|^)[^,]+(?=,)),*(?=.*?\1)|(^,+|,+$)|,+(?=,)", "")

    ; This latest RE example overcomes the error of the missing "D85" because "D85" mistakenly matched "CD85".
    $a[$i] = StringRegExpReplace($a[$i], "((?<=^|,)[^,]+(?=,)),*(?=.*?,\1,?)|(^,+|,+$)|,+(?=,)", "") ; <----------- The working Reg. Exp.
Next

_DebugArrayDisplay($a)

 

Edited by Malkey
I thought mikell's following example was the way to go. But now, I managed to overcome the problem that using "\b" created.
Link to comment
Share on other sites

Not as great for long strings, but anyways add in to the mix:

Edit: Now as Fast as doing this with  Scripting.Dictionary

Func StringUnique_Delim($sString = "", $delim = ",")
    Return StringTrimLeft(StringRegExpReplace($delim & $sString, '(' & $delim & '[^' & $delim & ']+(?=' & $delim & '))(?=.*\1)|' & $delim & '(?=' & $delim & ')', ""), 1)
EndFunc   ;==>StringUnique_Delim

 

Edited by Deye
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...