Community discussions

MikroTik App
 
lanhampr
newbie
Topic Author
Posts: 34
Joined: Wed Aug 04, 2021 7:18 pm

Finding duplicates in array

Wed Jan 11, 2023 4:01 pm

I have searched the internets but cannot find an example of a script to find duplicates in a single dimension array. I see there may be a count or count-only statement but have not been able to figure it out. I create an array of values, some of these values may be duplicates and I would like to find them. The actual script will be to create an array of Agent Remote ID's, find the duplicates send an email and/or add the IP's to an address-list for other action.

Thanks.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Finding duplicates in array

Wed Jan 11, 2023 4:27 pm

It's easy

Take first element of array A, compare with all the other elements on temporary array B,
if not already exist the same on temporary array B, copy on temporary array B,
repeat for each record of array A.

At the end copy the temporary array B over array A

Done.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Finding duplicates in array

Wed Jan 11, 2023 4:57 pm

search tag # rextended remove duplicates on array

I have searched the internets
wow, how many?


example code

{
:local arrA {0;1;1;2;3;5;8;13;21;34;55;89;144;0;1;1;2;3;5;8;13;21;34;55;89;144}
:local arrB [:toarray ""]

:put $arrA

:foreach itemA in=$arrA do={
    :local chk true
    :foreach itemB in=$arrB do={:if ($itemA = $itemB) do={:set chk false}}
    :if ($chk) do={:set arrB ($arrB,$itemA)}
}
:set arrA $arrB

:put $arrA
}

function code

:global remduponarray do={
    :local retarr [:toarray ""]
    :foreach srcitem in=$1 do={
        :local copy true
        :foreach dstitem in=$retarr do={:if ($srcitem = $dstitem) do={:set copy false}}
        :if ($copy) do={:set retarr ($retarr,$srcitem)}
    }
    :return $retarr
}


# example:
{
:local arraywithdup {0;1;1;2;3;5;8;13;21;34;55;89;144;0;1;1;2;3;5;8;13;21;34;55;89;144}
:put [$remduponarray $arraywithdup]

}
 
lanhampr
newbie
Topic Author
Posts: 34
Joined: Wed Aug 04, 2021 7:18 pm

Re: Finding duplicates in array

Wed Jan 11, 2023 5:19 pm

Thanks, here is what I came up with:

:local ari {1;1;2;3;4;5;4};
:local aritmp {""};
:local aridups {""};

:log info "Array is:";
:log info $ari;

:local values {10;20;20;20;30;40}
:foreach v in=$ari do={
:local p [:find $aritmp $v]
:if ([:type $p]="nil") do={
:log info "$v is not in array"
:set aritmp ($aritmp, $v)
} else={
:log info "$v is in array"
:set aridups ($aridups, $v)
}
}
:log info "Duplicates:";
:log info $aridups;
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Finding duplicates in array

Wed Jan 11, 2023 5:35 pm

So, do you want to know which and how many duplicates are in the array?

This returns an array with just the duplicate values and how many times they are duplicated.

function code

:global countduponarray do={
    :local retarr [:toarray ""]
    :foreach srcitem in=$1 do={
        :local equal false
        :foreach dstitem in=$1 do={:if ($srcitem = $dstitem) do={:set equal true}}
        :if ($equal) do={:set ($retarr->"$[:tostr $srcitem]") ([:tonum ($retarr->"$[:tostr $srcitem]")] +1)}
    }
    :return $retarr
}


# example:
{
:local arraywithdup {0;1;1;2;3;5;8;13;21;34;55;89;144;0;1;1;2;3;5;8;13;21;34;55;89;144}
:put [$countduponarray $arraywithdup]

}

return this array:
0=2;1=4;13=2;144=2;2=2;21=2;3=2;34=2;5=2;55=2;8=2;89=2
 
lanhampr
newbie
Topic Author
Posts: 34
Joined: Wed Aug 04, 2021 7:18 pm

Re: Finding duplicates in array

Wed Jan 11, 2023 6:19 pm

internetS (joke)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Finding duplicates in array

Wed Jan 11, 2023 6:22 pm

internetS (joke)
;)


Let me know if the functions is what you need at the end.
 
lanhampr
newbie
Topic Author
Posts: 34
Joined: Wed Aug 04, 2021 7:18 pm

Re: Finding duplicates in array

Wed Jan 11, 2023 7:01 pm

A bit off topic but pertains to what I am trying to accomplish. I swear I used this command yesterday but now get a syntax error.

:foreach i in=[find server="NOC3-CGNAT"] do={ :set array ($array, [get $i agent-remote-id])}

I get a syntax error at "array".
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Finding duplicates in array

Wed Jan 11, 2023 8:24 pm

I do not see the rest of the script,
"array" is already defined globally or locally?
(is better not use some words, like array, number, string, etc. ...)
 
lanhampr
newbie
Topic Author
Posts: 34
Joined: Wed Aug 04, 2021 7:18 pm

Re: Finding duplicates in array

Thu Jan 12, 2023 12:23 am

I do not see the rest of the script,
"array" is already defined globally or locally?
(is better not use some words, like array, number, string, etc. ...)
Agreed, I was just doing some quick tests and debugging. array was defined as local array {"}. I got it working, not really sure how. Working with Mikrotik scripts is a bit difficult. The console will show errors in the syntax, winbox script box does not.

I added the Mikrotik script for Visual Studio on my Mac. Only problem I have now is that I have to use Wine to use winbox64.exe and after a few iterations copy and paste no longer work until I close and open all windows.

Who is online

Users browsing this forum: No registered users and 21 guests