Jump to content

How do I delete duplicate array items out of an array?


Recommended Posts

I created an array using the _FileListToArrayEx UDF here: http://www.autoitscript.com/forum/index.ph...st&p=246431

Example:

Func OnList()
    GUICtrlSetData($lblStatus, "Getting Providers...")
    $dictationPath = @UserProfileDir & "\Desktop\Dictation\"
    $provArray = _FileListToArrayEx($dictationPath, '*.dss')
        If (Not IsArray($provArray)) Or (@error = 1) Then
            MsgBox(262208, "List Providers Error", "No Files\Folders Found.   ")
        Return
    EndIf
    GUICtrlSetData($lstProviders, "") ; set list to empty.
    _ArraySort($provArray)
    For $x = 1 To $provArray[0] ; for loop to place the files in the list.
        GUICtrlSetData($lstProviders, (StringLeft($provArray[$x], 3)) & "|", 1) ; string trim the last 4 characters
    Next
EndFunc

What I'm left with in my list box is a lot of [ABC, ABC, JPR, JPR, JPR, DDA, BCC, BCC] type of data. What I want to know is how I delete the duplicate array items, so my array is like: [ABC, JPR, DDA, BCC].

Thanks.

Link to comment
Share on other sites

take the array and have it made as a string. take one array element at a time and compare i to the string using "StringInStr" this will tell you how many times it appears if more than once. now if it appears more than once use "StringReplace" to replace them however many times it is over 1. then use "StringSplit" to build yourself a new array from the finished string.:rolleyes:

Link to comment
Share on other sites

I tried implementing _ArrayUnique in my script, but it keeps giving me an output in my array of 0. I see how to use it when reading a file, but I can't get it to work with my example code above. Could somebody give me an example of how to use it when reading a directory of files that are trimmed to their first 3 charachers.

Link to comment
Share on other sites

  • Moderators

I tried implementing _ArrayUnique in my script, but it keeps giving me an output in my array of 0. I see how to use it when reading a file, but I can't get it to work with my example code above. Could somebody give me an example of how to use it when reading a directory of files that are trimmed to their first 3 charachers.

You'll have to create a new Array or use it after _ArraySort() going off your example.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

This is the code I created, but it only puts one item in the list and I should have several.

Func OnList()
    GUICtrlSetData($lblStatus, "Getting Providers...")
    $dictationPath = @UserProfileDir & "\Desktop\Dictation\"
    $provArray = _FileListToArrayEx($dictationPath, '*.dss')

    If (Not IsArray($provArray)) Or (@error = 1) Then
            MsgBox(262208, "List Providers Error", "No Files\Folders Found.   ")
        Return
    EndIf
    
    GUICtrlSetData($lstProviders, "") ; set list to empty.
    For $x = 1 To $provArray[0] ; for loop to place the files in the list.
        $pArray = StringLeft($provArray[$x] & "|" , 3)
    Next
        _ArrayUnique($pArray)
        GUICtrlSetData($lstProviders, $parray, 1) ; string trim the last 4 characters ( .dss )
EndFunc
Link to comment
Share on other sites

  • Moderators

This is the code I created, but it only puts one item in the list and I should have several.

Func OnList()
    GUICtrlSetData($lblStatus, "Getting Providers...")
    $dictationPath = @UserProfileDir & "\Desktop\Dictation\"
    $provArray = _FileListToArrayEx($dictationPath, '*.dss')

    If (Not IsArray($provArray)) Or (@error = 1) Then
            MsgBox(262208, "List Providers Error", "No Files\Folders Found.   ")
        Return
    EndIf
    
    GUICtrlSetData($lstProviders, "") ; set list to empty.
    For $x = 1 To $provArray[0] ; for loop to place the files in the list.
        $pArray = StringLeft($provArray[$x] & "|" , 3)
    Next
        _ArrayUnique($pArray)
        GUICtrlSetData($lstProviders, $parray, 1) ; string trim the last 4 characters ( .dss )
EndFuncoÝ÷ Ûú®¢×¢èZ½ëhªê-­ç+^ªê-¶®¶²ü¨º­jg¬±¨nZ~׫jëh×6    For $x = 1 To $provArray[0] ; for loop to place the files in the list.
        $pArray = StringLeft($provArray[$x] & "|" , 3)
    Next
    MsgBox(64, 'Info', $pArray)oÝ÷ Ù.ßÙe±ç­«r¢ê'*n¶Ø^ªê-«-ªê-zW¦z{hMú¦º/ºÚÊ)íêÞb·õÙ¨­ëajybérÁ©í¶h²¶§X¤y«­¢+ØU%
ÑɱMÑÑ ÀÌØí±±MÑÑÕÌ°ÅÕ½ÐíÑÑ¥¹Aɽ٥É̸¸¸ÅÕ½Ðì¤(ÀÌØí¥ÑÑ¥½¹AÑ ôUÍÉAɽ¥±¥ÈµÀìÅÕ½ÐìÀäÈíͭѽÀÀäÈí¥ÑÑ¥½¸ÀäÈìÅÕ½Ðì(ÀÌØíÁɽÙÉÉäô}¥±1¥ÍÑQ½ÉÉåà ÀÌØí¥ÑÑ¥½¹AÑ °Ìä쨹ÍÌÌäì¤((%¡9½Ð%ÍÉÉä ÀÌØíÁɽÙÉÉ䤤=È¡ÉɽÈôĤQ¡¸(5Í    ½à ÈØÈÈÀà°ÅÕ½Ðí1¥ÍÐAɽ٥ÉÌÉɽÈÅÕ½Ðì°ÅÕ½Ðí9¼¥±ÌÀäÈí½±É̽չ¸ÅÕ½Ðì¤(IÑÕɸ(¹%(ÀÌØíÁÉÉäôÌäìÌäììÀÌØíÁÉÉä¡ÌѼ±É¥ÉÍÐÍ¥¹ÉÕÍ¥¹µÀìô±½ÜѼѼ¥Ð(½ÈÀÌØíàôÄQ¼ÀÌØíÁɽÙÉÉålÁtì½È±½½ÀѼÁ±Ñ¡¥±Ì¥¸Ñ¡±¥Íи(ÀÌØíÁÉÉäµÀìôMÑÉ¥¹1Ð ÀÌØíÁɽÙÉÉålÀÌØíátµÀìÅÕ½ÐíðÅÕ½Ðì°Ì¤µÀì
¡È Ĥì9½Ñ¥µÀìôѼ ±µ¹ÐѼÀÌØíÁÉÉä¹¹½Ñ¥µÀì
¡È ĤѼÕͱ¥µ¥ÑȽÈÑ¡¹áÐÉÉä(9áÐ(ÀÌØíÁÉÉäôMÑÉ¥¹MÁ±¥Ð¡MÑÉ¥¹QÉ¥µI¥¡Ð ÀÌØíÁÉÉ䤰
¡È Ĥ¤íMÑÉ¥¹QÉ¥µI¥¡Ð ¤¥ÌÕÍѼÐÉ¥½Ñ¡±Íб¥µ¥ÑȽÈÝÝ¥±°¡Ù¸µÁÑä±µ¹Ð¸(}ÉÉåU¹¥ÅÕ ÀÌØíÁÉÉä°
¡È Ĥ°Ä¤íÁ¹¥¹½¸Ý¡¥ }ÉÉåU¹¥ÅÕ ¤å½ÔÉÕÍ¥¹°å½Ôµä¡ÙѼ¼èÀÌØíÁÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÁÉÉä¤(í9½ÜÝ¡ÙѼ±½½ÀÑ¡É½Õ  ½¹Ñ¼µ­¥Ð½¹ÍÑÉ¥¹Ñ¼Ñ¼Ñ¡ÀÌØí±ÍÑÁɽ٥É̸¸¸½½ÕÉÍ°$¸½¹±äÍÍյѡÐÀÌØí±ÍÑAɽ٥É̸̥¥Ð(U%
ÑɱMÑÑ ÀÌØí±ÍÑAɽ٥ÉÌ°ÌäìÌäì¤íµÁÑäÑ¡½¹Ñɽ°ÀÌØí±ÍÑAɽ٥ÉÌ(ÀÌØíÍ!½±9Ý1¥ÍÐôÌäìÌäìí]Ý¥±°ÕÍÑ¡¥ÌѼ¡½±Ñ¡¹Ü±¥Íи(½ÈÀÌØí¥
ôÄQ¼U    ½Õ¹ ÀÌØíÁÉÉ䤴Ä(ÀÌØíÍ!½±9Ý1¥ÍеÀìôÀÌØíÁÉÉålÀÌØí¥

tµÀì
I1(9áÐ(ÀÌØíÍ!½±9Ý1¥ÍÐôMÑÉ¥¹QÉ¥µI¥¡Ð ÀÌØíÍ!½±9Ý1¥ÍаȤí
I1¥ÌÈ
¡ÉÌ°¡ÙѼÑÉ¥´Ñ¡Ð½Ñ¡¹Ì(U%
ÑɱMÑÑ ÀÌØí±ÍÑAɽ٥ÉÌ°ÀÌØíÍ!½±9Ý1¥ÍФ)¹Õ¹
Hope that gives you more of an understanding.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

How about something like this:

$bound = UBound($aArray - 1)                              ;sets loop boundaries
$j = 0
$i = 0

While $i < $bound
    While $j < $bound
        If $i <> $j Then                                  ;don't check element $i against element $i
            If $aArray[$i] == $aArray[$j] Then
                _ArrayDelete($aArray, $j)                 ;delete the element if a match is found
                $bound = UBound($aArray - 1)              ;set a new loop boundary based on new array size
            EndIf       
        EndIf
        $j += 1
    WEnd
    $j = 0
    $i += 1
WEnd

Edit: tags

As well, the same thing can be done with For/Next loops.

Edited by mikehunt114
IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

  • Moderators

How about something like this:

$bound = UBound($aArray - 1)                              ;sets loop boundaries
$j = 0
$i = 0

While $i < $bound
    While $j < $bound
        If $i <> $j Then                                  ;don't check element $i against element $i
            If $aArray[$i] == $aArray[$j] Then
                _ArrayDelete($aArray, $j)                 ;delete the element if a match is found
                $bound = UBound($aArray - 1)              ;set a new loop boundary based on new array size
            EndIf       
        EndIf
        $j += 1
    WEnd
    $j = 0
    $i += 1
WEnd

Edit: tags

As well, the same thing can be done with For/Next loops.

Trying to re-invent the wheel? :rolleyes:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I didn't actually follow all the postings here, I just wanted to dive back into arrays after a long absence :rolleyes: So what you see is what came outta the cobwebs.

Edit: typos galore

Edited by mikehunt114
IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

GUICtrlSetData($lblStatus, "Getting Providers...")
    $dictationPath = @UserProfileDir & "\Desktop\Dictation\"
    $provArray = _FileListToArrayEx($dictationPath, '*.dss')

    If (Not IsArray($provArray)) Or (@error = 1) Then
            MsgBox(262208, "List Providers Error", "No Files\Folders Found.   ")
        Return
    EndIf
    $pArray = '';$pArray has to be declared first since are using &= below to add to it
    For $x = 1 To $provArray[0] ; for loop to place the files in the list.
        $pArray &= StringLeft($provArray[$x] & "|" , 3) & Chr(1); Notice &= to add each element to $pArray and notice & Chr(1) to use a delimiter for the next array
    Next
    $pArray = StringSplit(StringTrimRight($pArray), Chr(1));StringTrimRight() is used to get rid of the last delimiter or we will have an empty element.
    _ArrayUnique($pArray, Chr(1), 1);Depending on which _ArrayUnique() you are using, you may have to do:  $pArray = _ArrayUnique($pArray)
    ;Now we have to loop through each one to make it one string to add to the $lstproviders... of course, I can only assume that $lstProviders is an edit
    GUICtrlSetData($lstProviders, '');Empty the control $lstProviders
    $sHoldNewList = '';We will use this to hold the new list.
    For $iCC = 1 To UBound($pArray) - 1
        $sHoldNewList &= $pArray[$iCC] & @CRLF
    Next
    $sHoldNewList = StringTrimRight($sHoldNewList, 2);@CRLF is 2 Chars, have to trim the fat off the ends
    GUICtrlSetData($lstProviders, $sHoldNewList)
EndFunc
While I don't understand everything, yet, I did get it working, using your example. I really need to learn more about arrays and loops.
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...