Community discussions

MikroTik App
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Convert Identity name to uppercase

Sat Oct 29, 2022 3:06 pm

Hello everyone,

Would it be possible to create a function (or script) to convert the Identity name to uppercase?

Examples:

Identity: Office => OFFICE
Identity: WorkShop => WORKSHOP
Identity: Design Department => DESIGN_DEPARTMENT
(In the case of compound words, join them, for example, with an underscore)

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

Re: Convert Identity name to uppercase

Sun Oct 30, 2022 1:13 pm

Yes, is possible.

If you check character by character inside a string, you can convert the characters to uppercase with this:

viewtopic.php?f=9&t=75555&p=876693#p876693

The script can be adapted for replace for example space with _ but at the time I write do not have the time.

If you keep this post up on next days someone can help you, or I can find the time to write the script.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert Identity name to uppercase

Sun Oct 30, 2022 1:34 pm

OK, thanks for responding @rextended,

Yes, I know your "Rextended Fragments of Snippets" script collection and specifically the script you point out to try to adapt it but my scripting knowledge is very basic, although I have learned quite a lot with this forum.

No worries, when you can, in the meantime I will try to understand it to adapt it to what I need.

Thanks!

BR.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert Identity name to uppercase

Sun Oct 30, 2022 7:55 pm

Regarding the examples:

1) Identity: Office => OFFICE
2) Identity: WorkShop => WORKSHOP
3) Identity: Design Department => DESIGN DEPARTMENT

I have already solved it

Case 3:
{
# Removes spaces in System > Identity and replaces them with underscores.
# Identity example: Design Department 
:local sysname [/system identity get name]
:if ([:find $sysname " "] !=0) do={
    :local name $sysname;
    :local newname "";
        :for i from=0 to=([:len $name]-1) do={ :local tmp [:pick $name $i];
        :if ($tmp !=" ") do={ :set newname "$newname$tmp" }
        :if ($tmp =" ") do={ :set newname "$newname_" }
        }
    :set sysname $newname;
}
:put $sysname
}
Result: Design_Department
Case 1 & 2:
{
:global strcase do={
	:local toreplaceL {"A"="a";"B"="b";"C"="c";"D"="d";"E"="e";"F"="f";"G"="g";"H"="h";"I"="i";"J"="j";"K"="k";"L"="l";"M"="m";"N"="n";"O"="o";"P"="p";"Q"="q";"R"="r";"S"="s";"T"="t";"U"="u";"V"="v";"X"="x";"Z"="z";"Y"="y";"W"="w"};
	:local toreplaceU {"a"="A";"b"="B";"c"="C";"d"="D";"e"="E";"f"="F";"g"="G";"h"="H";"i"="I";"j"="J";"k"="K";"l"="L";"m"="M";"n"="N";"o"="O";"p"="P";"q"="Q";"r"="R";"s"="S";"t"="T";"u"="U";"v"="V";"x"="X";"z"="Z";"y"="Y";"w"="W"};
	:local toreplace $toreplaceL;
	:local str $1;
	:local newStr;
	:local newChar;

	:if ($2 = "-u") do={
		:set toreplace $toreplaceU;
	}

	:for i from=0 to=([:len $str] - 1) do={
	    :local char [:pick $str $i];
	    :set newChar ($toreplace->$char);
	  
	    :if ([:typeof $newChar] = "str") do={
	    :set char $newChar;
	    }
	    :set newStr ($newStr . $char);
	}
	:return $newStr;
}

:global tolower do={ :global strcase; return [$strcase $1]};
:global toupper do={ :global strcase; return [$strcase $1 -u]}
}
Result:

Original Identity: Design Department

:put [$toupper $sysname]
DESIGN DEPARTMENT

:put [$tolower $sysname]
design department

What is your opinion?

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

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 11:23 am

What is your opinion?
Welldone!

I write my alternative script,
i wanted to do it to convert text to GSM alphabet, but i think it can be used for that too.

If I have not misunderstood, the purpose is to read the names coming from "system identity" (or DHCP Leases, or IP Neighbors) and convert them all to UPPERCASE and where there are spaces enter _
And the unsupported chars on name, like "jack's pc" must be converted to "jack_s_pc"?
(space) => _ (underscore)
- = - (minus)
. = . (dot)
0-9 = 0-9
a-z => A-Z
_ = _ (underscore)
A-Z = A-Z
all_the_others => _ (underscore)
Did I get it right?

v space
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

converted to

_____________-._0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______ABCDEFGHIJKLMNOPQRSTUVWXYZ____
all the other characters from 0x00 to 0x19 and from 0x127 to 0x255 are converted to _
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert Identity name to uppercase  [SOLVED]

Mon Oct 31, 2022 12:27 pm

search tag # rextended codepage conversion

This is the script:
:global convchr do={
    :local chr $1
    :if (([:typeof $chr] != "str") or ($chr = "")) do={ :return "" }
    # ascii length > conv length because escaped " $ \ and question mark
    :local ascii " !\"#\$%&'()*+,-./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
    :local conv  "_____________-._0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______ABCDEFGHIJKLMNOPQRSTUVWXYZ____"
    :local chrValue [:find $ascii [:pick $chr 0 1] -1]
    :if ([:typeof $chrValue] = "num") do={
        :return [:pick $conv $chrValue ($chrValue + 1)]
    } else={
        :return "_"
    }
}

:global convstr do={
    :global convchr
    :local string $1
    :if (([:typeof $string] != "str") or ($string = "")) do={ :return "" }
    :local lenstr [:len $string]
    :local constr ""
    :for pos from=0 to=($lenstr - 1) do={
        :set constr "$constr$[$convchr [:pick $string $pos ($pos + 1)]]"
    }
    :return $constr
}

:put [$convstr (" !\"#\$%&'()*+,-./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")]

result:

terminal code

[9876wech2@#AP Ingresso] > :put [$convstr (" !\"#\$%&'()*+,-./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")]
_____________-._0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______ABCDEFGHIJKLMNOPQRSTUVWXYZ____
[9876wech2@#AP Ingresso] > 

Adding at the start of both ascii and conv strings the characters \t\n\r (0x09 tab 0x0A new line 0x0D return carriage)
can be used for convert also files with "tabs" and "returns" (limited to ~4k)
Last edited by rextended on Mon Oct 31, 2022 7:05 pm, edited 5 times in total.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 4:58 pm

Did I get it right?
Yes, of course!
;)

BR.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 6:30 pm

Hi @rextended,

It doesn't let me declare global variables by putting the code inside a script and executing it.

I get no result when executing (for example) the line:
:put [$convstr "DeSpAcHo"]

However if I declare the global variables from the terminal, inside {...} it works correctly.

The ideal would be to run it from a script through Scheduled every time I reboot the router, right?

Any idea?


BR.
Last edited by diamuxin on Mon Oct 31, 2022 7:55 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 6:54 pm

Ok, a ROS bug.... please REMOVE on your previous post all scripting parts, include the quote of my post. or is a mess on search,
I modify previous post with the fix
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 7:54 pm

Done!

BR.
Last edited by BartoszP on Mon Oct 31, 2022 11:53 pm, edited 1 time in total.
Reason: removed excessive quotting
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 9:34 pm

You have tested fixed version? Now work as expected?
Last edited by BartoszP on Mon Oct 31, 2022 11:53 pm, edited 1 time in total.
Reason: removed excessive quotting
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Convert Identity name to uppercase

Mon Oct 31, 2022 11:16 pm

You have tested fixed version? Now work as expected?
Yes, everything works fine now.
Thank you very much.

BR.

Who is online

Users browsing this forum: Ahrefs [Bot], GoogleOther [Bot] and 18 guests