How delete duplicates from array?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Alina
Newbie
Posts: 17
Joined: Sun Jul 04, 2021 9:02 am

How delete duplicates from array?

Post by Alina » Wed Jul 28, 2021 5:25 am

Good day!
I have an Array.

Code: Select all

GetClipBoard>List,0
Separate>List,CRLF,Array1
How can I delete duplicates from the array and stay only uniq values?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How delete duplicates from array?

Post by Grovkillen » Wed Jul 28, 2021 7:00 am

Loop through the array and add new value to another string.

Code: Select all

GetClipBoard>List,0
Separate>List,CRLF,Array1
Let>uniqueList=
Let>k=0
Repeat>k
  Let>k=k+1
  Let>tempValue=Array1_%k%  
  Position>tempValue,uniqueList,1,foundValue,False
  If>foundValue=0
    Let>uniqueList=%uniqueList%|
  EndIf>
Until>k=Array1_count
Separate>uniqueList,|,Array2
The last value of the Array2 will be empty though. And please be aware that I wrote this on the phone and on top of my head... Syntax might be remembered incorrectly etc.
Let>ME=%Script%

Running: 15.0.24
version history

Alina
Newbie
Posts: 17
Joined: Sun Jul 04, 2021 9:02 am

Re: How delete duplicates from array?

Post by Alina » Wed Jul 28, 2021 7:17 am

Thank you!

Program writes "Error in Position Command" in this string:
Position>tempValue,uniqueList,1,foundValue,False

And in this string uniqueList= must be empty?
Let>uniqueList=

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How delete duplicates from array?

Post by Grovkillen » Wed Jul 28, 2021 7:22 am

Yes you need to declare the empty string first. Look in the manual to get the correct syntax for the position command.
Let>ME=%Script%

Running: 15.0.24
version history

Alina
Newbie
Posts: 17
Joined: Sun Jul 04, 2021 9:02 am

Re: How delete duplicates from array?

Post by Alina » Wed Jul 28, 2021 11:23 am

Mistake's disappeared.
But result of macros is empty unfortunately,
MessageModal>Array2_%x% - this string gives empty.

Code: Select all

GetClipBoard>List,0
Separate>List,CRLF,Array1

Let>uniqueList=

Let>k=0
Repeat>k
Let>k=k+1

Let>tempValue=Array1_%k%
  Position>tempValue,uniqueList,1,foundValue,FALSE
  If>foundValue=0
    Let>uniqueList=%uniqueList%|
  EndIf>

MessageModal>Array1_%k%
Until>k=Array1_count



Separate>uniqueList,|,Array2
Let>x=0
Repeat>x
Let>x=x+1
MessageModal>Array2_%x%
Until>x=Array2_count

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How delete duplicates from array?

Post by Grovkillen » Wed Jul 28, 2021 12:16 pm

Use the variable inspector and step though your script using F8.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: How delete duplicates from array?

Post by Dorian (MJT support) » Wed Jul 28, 2021 12:41 pm

Grovkillen is on his phone, and I must say he did a better job than I could have done off the top of my head.

I think part of the problem was that %tempValue% wasn't being appended to %uniqueList%. The Position error you mentioned happens when the last array item is blank, so if you're selecting a blank line at the end of your selection and then copying it to the clipboard.

Code: Select all

GetClipBoard>List,0
Separate>List,CRLF,Array1
Let>uniqueList=
Let>k=0

Repeat>k
  Let>k=k+1
  Let>tempValue=Array1_%k%
  Position>tempValue,uniqueList,1,foundValue,FALSE
  If>foundValue=0
    Let>uniqueList=%tempValue%|%uniqueList%
  EndIf>
  MessageModal>Array1_%k%
Until>k=Array1_count

Separate>uniqueList,|,Array2
Let>x=0
Repeat>x
Let>x=x+1
MessageModal>Array2_%x%
Until>x=Array2_count
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How delete duplicates from array?

Post by Grovkillen » Wed Jul 28, 2021 1:40 pm

Ah, that was one very important error on my part :D
Let>ME=%Script%

Running: 15.0.24
version history

Alina
Newbie
Posts: 17
Joined: Sun Jul 04, 2021 9:02 am

Re: How delete duplicates from array?

Post by Alina » Wed Jul 28, 2021 5:12 pm

Grovkillen and Dorian Thank you very much! :D
Grovkillen, macros is great and very useful :idea: Dorian, without your help I could not run macros :!: .

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: How delete duplicates from array?

Post by Dorian (MJT support) » Thu Jul 29, 2021 9:43 am

Here's a self contained version in case it helps anyone else.

I've tweaked it a bit so if the last item in Array1 is blank, it subtracts 1 from Array1_Count. This means it won't process the last item if it's blank, and we won't get the Pos error.

Also seeing as the last item in Array2 is always blank, we can always ignore the last item. We can do that with a Complex Expression. As you can see the last line is

Code: Select all

Until>x={(%Array2_count%-1)}

Code: Select all

/*
TheList:
fish
horse
dog
horse
fish
*/

LabelToVar>TheList,List
PutClipBoard>List

GetClipBoard>List,0
Separate>List,CRLF,Array1

//If last item is blank, ignore it
Let>LastArrayItemNum=Array1_Count
if>Array1_%LastArrayItemNum%=
  Let>Array1_Count=Array1_Count-1
endif

Let>uniqueList=
Let>k=0

Repeat>k
  Let>k=k+1
  Let>tempValue=Array1_%k%
  Position>tempValue,uniqueList,1,foundValue,FALSE
  If>foundValue=0
    Let>uniqueList=%tempValue%|%uniqueList%
  EndIf>
  MessageModal>Array1_%k%
Until>k=Array1_count

Separate>uniqueList,|,Array2
Let>Array2_count=Array2_count-1

Let>x=0
Repeat>x
Let>x=x+1
MessageModal>Array2_%x%
Until>x={(%Array2_count%-1)}
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts