Community discussions

MikroTik App
 
DyadyaGenya
Member Candidate
Member Candidate
Topic Author
Posts: 222
Joined: Mon May 08, 2023 10:34 pm

Searching for words in an array.

Sat Nov 25, 2023 7:02 pm

With some free time appearing, I've started trying to recall scripts. Unfortunately, I realize that I've forgotten a lot. For instance, I can't remember/write a simple function to find words in an array, although I think I did something like that in the spring.
:local months [ :toarray "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"];
:local searchWord "nov";
:local result [:find $months $searchWord]
:put [ :typeof $result]
:if ($result > 0) do={
    :put ("Word found at position " . $result)
} else={
    :put "Word not found"
}
I use version 7.9.1 and, unfortunately, I don’t want to install version 7.11, where the “convert” command was introduced.
 
DyadyaGenya
Member Candidate
Member Candidate
Topic Author
Posts: 222
Joined: Mon May 08, 2023 10:34 pm

Re: Searching for words in an array.

Sat Nov 25, 2023 8:38 pm

Apparently in Mikrotik there is no direct way to search using ":find", although the manual says what you should search for. Therefore, at the moment the only solution is this:
:local months [ :toarray "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"];
:local searchWord "nov";
:foreach k,v in=$months do={
    :if ($v=$searchWord) do={:put "yes"};
    }
I hope that I'm wrong and in Mikrotik there is a way to search in an array using "find"
 
DyadyaGenya
Member Candidate
Member Candidate
Topic Author
Posts: 222
Joined: Mon May 08, 2023 10:34 pm

Re: Searching for words in an array.

Sat Nov 25, 2023 8:49 pm

I asked the question myself and answered it myself))) Again, my inattention and parentheses. Parentheses in Mikrotik constantly confuse me)))
This is how it will work:
:local months [ :toarray "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"];
:local searchWord "nov";
:local result ( [:find $months $searchWord] );
:put [ :typeof $result]
:if ($result > 0) do={
    :put ("Word found at position " . $result)
} else={
    :put "Word not found"
}
Although this is a more correct option:
:local months [ :toarray "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"];
:local searchWord "jan";
:local result ( [:find $months $searchWord] );
:put [ :typeof $result]
:if ( [ :typeof $result] = "num") do={
    :put "yes"
    } else={
    :put "no"
    }
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12558
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Searching for words in an array.

Sat Nov 25, 2023 9:24 pm

The script on OP work correcly on both 6.48.7 and 7.12,
so, no superfluos ( ) needed

The correct way is like this:

correct example code

:local months     [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local searchWord "nov"
:local result     [:find $months $searchWord -1]
:if ([:typeof $result] = "num") do={
    :put "Word found at position $result"
} else={
    :put "Word not found"
}
 
DyadyaGenya
Member Candidate
Member Candidate
Topic Author
Posts: 222
Joined: Mon May 08, 2023 10:34 pm

Re: Searching for words in an array.

Sun Nov 26, 2023 12:25 am

correct example code

:local result     [:find $months $searchWord -1]
-1 was unnecessary))) I understand that you wanted to count the months, but I took the months only as an example, without being tied to their real number.
The script on OP work correcly on both 6.48.7 and 7.12,
so, no superfluos ( ) needed
But this is very strange. I tried it now and it really works. But before that it didn't work for me. I copied it as is. At the same time, the type of the result variable was printed as "nil". If I had typed it as "num", I would have decided that there was an error in my “if” condition. It really is there, but the most important thing is that I received “nil”. And I cannot understand why this was so.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4325
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Searching for words in an array.

Sun Nov 26, 2023 12:32 am

:find with an array type finds the index of element that exactly match — it doesn't search the array for strings (unless you use a ":tostr" BEFORE the :find). The :find with an array, correctly, returns "nil", if what your seaching does not exactly match one of the items in the array.

:global months [ :toarray "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"];
:put [:find $months "feb"]
# 1
:put [:typeof [:find $months "jan, f"]]
# nil

Why don't you provide the example of what you're trying to do. Or if you want to search strings, then don't make it an array type.
"-1" isn't very helpful to know...
 
DyadyaGenya
Member Candidate
Member Candidate
Topic Author
Posts: 222
Joined: Mon May 08, 2023 10:34 pm

Re: Searching for words in an array.

Sun Nov 26, 2023 4:49 pm

Why don't you provide the example of what you're trying to do. Or if you want to search strings, then don't make it an array type.
I'm afraid I'm translating something wrong again. Actually, I gave an example of exactly how I wanted to get the result. It completely satisfies me. But as questions and clarifications arise, I also have options for what I would like to see. So, in the first case, I wanted to find a complete match of the search phrase to some array element, assuming that one of the array elements completely matches the search phrase, even if this phrase is a large string with many punctuation marks. And my last search option solved this problem. In fact, Rex's version is the same, only he demonstrated that my version can work without parentheses.
But I don't quite understand what it means:
if you want to search strings, then don't make it an array type.
Let's say I want to find a phrase/word that is only part of a phrase in some array element.
:local months [ :toarray "jan, feb, febr"];
:put [:find $months "fe"]
And this interests me especially since you said:
The :find with an array, correctly, returns "nil", if what your seaching does not exactly match one of the items in the array.
Because I thought "nil" is returned on any mismatch. But you sounded like there was a difference in whether it partially did not coincide or did not coincide completely. Again, perhaps these are all just translation errors.

If we return to my tasks and your examples, then perhaps you meant that I consider a record of the form
:local months [ :toarray "jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec"];
as a string, and I want to find the string
"jan, f"
I want to assure you that I understand that the first thing is not a string, but array elements. And that the search is performed strictly within each element, so if I do
:put [:typeof [:find $months "jan, f"]]
Then we will get "nil"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12558
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Searching for words in an array.

Mon Nov 27, 2023 1:27 am

But before that it didn't work for me. I copied it as is. At the same time, the type of the result variable was printed as "nil". If I had typed it as "num", I would have decided that there was an error in my “if” condition. It really is there, but the most important thing is that I received “nil”. And I cannot understand why this was so.
I can't reconstruct what you did, but for sure you made mistakes.
At least do not return "nil" but "nothing" because you forget the { } on terminal...
Or return "nil" because you write "nov"ember or another month with your language and the month is not found...
Or you write "nov " with the useless space...
Etc...

You tell me that -1 is superfluous and then you fill the array with superfluous spaces and use ";" unnecessarily, so...
The -1 becomes superfluous when you know what you're doing, so it's not superfluous for you yet...

Who is online

Users browsing this forum: No registered users and 2 guests