Jump to content

UNIQUE ELEMENTS IN ARRAY - (Moved)


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 post
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 post
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 post
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 post
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 post
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 post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By TheAlienDoctor
      Hi, I was looking into creating a script that would detect if a file exists, then move it (and in some cases rename it, depending on the file) as well as write to a log file.
      The issue is, there is a lot of files that need to be moved, sometimes some files will exist and others won't depending on the use-case. However if a file does exist, it will always be going into the same directory with the same name.
      Currently I have an array nested inside of the array, with each array inside that array having both the old and new directory, and then a For loop to actually run through and do the file transferring. The issue I am having is how to call the Array inside of the array, because how do I specify  which the old directory is and which the new is?
      Global $FileTransfer[2000] = [Global $Dir1[2] = ["original dir 1", "new dir 1"], Global $Dir2[2] = ["original dir 2", "new dir 2"]] For $FileTransfer = [0] To [1] Step +1 If FileExists({original dir}) Then FileMove({original dir}, new dir, 1) FileOpen("log.latest.txt", 1) FileWrite("log.latest.txt", "{original dir} found, moved it to new dir." & @CRLF) FileClose("log.latest.txt") Else FileOpen("log_latest.txt", 1) FileWrite("log_latest.txt", "{original dir} not found, ignoreing it." & @CRLF) FileClose("log_latest.txt") EndIf Next I have put what I want the old and new directory to be for each array in {}, so hopefully its easier to tell which part is working and whats not.

      I am still reasonably new to AutoIT, any help is appreciated. Thankyou
    • By arunkw
      I have a spreadsheet - daily routine which has two columns: activity and time as shown here
      | Activity             | Time     | |----------------------|----------| | Sleep               |  6:00 am | | Toilet              |  6:15 am | | Get ready for gym  |  6:30 am | | Exercise            |  7:50 am | | ... more things      |  9:00 pm | | ... still more       | 10:45 pm | | Sleep               |  6:00 am |   I wanted to find out, say in C1 which activity is current for me using now() I.e., if it’s 6:45am on my watch, it should show me Exercise  in C1 Thanks to Adam D. PE, this formula works like magic to get the result =VLOOKUP(MOD(NOW(),1),{B2:B,A2:A},2,1)   Now, I want to reproduce same result in autoit, how to do that? To have easy solution say, I copy-paste spreadsheet data in array directly in code, right? Use for loop and run the above vlookup function and show the answer using tooltip. How to achieve this? please help.  
    • By goku200
      I have an Autoit script that lists files from a folder into an array list. Is there a way to separate the filenames by an underscore and include the id, version, name and date into separate columns in Excel.
      Example of filename:
      12345_v1.0_TEST Name [12345]_01.01.2022.html
      12345 would be in one column
      v1.0 would be in another column
      TEST Name [12345] would be in another column
      01.01.2022 would be in another column
      .html would be in another column
      Note: filenames always change each day.
      Here is my code that lists the files into column C and then writes the column Headers into Column D, E, F, G. Just need some help with separating them into columns by the _ delimiter
       
    • By mLipok
      Usually when I collect data from DataBase I need to give EndUser a possibility to select rows which should be taken in the processing loop.
      I was searching on the forum and I'm not able to find any UDF or even example of how to select data from array.
      I have my own solutions but I think they are not worth posting on the forum as it is very old code and I am looking for a better solution.

      Could anybody point me to some examples/solutions ?

      Thank you in advance.
      @mLipok
    • By EmilyLove
      I have a string containing the full path of an executable and an array of executables without their paths. I am trying to compare the string to the list in the array and if a match is found, remove it from the array. The entry get removed from the array successfully, and after checking its return result, uses it to update the ubound if it succeeded, but it doesn't want to update to the new value. Any ideas what I am doing wrong? It acts like it is read-only.
      #include <Array.au3> #include <File.au3> Local $sApp_Exe = "F:\App\Nextcloud\nextcloud.exe" Local $aWaitForEXEX = [3, "Nextcloud.exe", "nextcloudcmd.exe", "QtWebEngineProcess.exe"] For $h = 1 To $aWaitForEXEX[0] If StringInStr($sApp_Exe, $aWaitForEXEX[$h]) <> 0 Then $iRet = _ArrayDelete($aWaitForEXEX, $h) If $iRet <> -1 Then $aWaitForEXEX[0] = $iRet ;this line doesn't work. $aWaitForEXEX[0] doesn't update and shortly gives Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: _ArrayDisplay($aWaitForEXEX) EndIf Next  
×
×
  • Create New...