Convert Identity name to uppercase

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.

Yes, is possible.

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

http://forum.mikrotik.com/t/i-did-it-script-to-compute-unix-time/68576/18

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.

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.

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.

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 _

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:
[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)

Yes, of course!
:wink:

BR.

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.

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

Done!

BR.

You have tested fixed version? Now work as expected?

Yes, everything works fine now.
Thank you very much.

BR.