Community discussions

MikroTik App
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Cleaning characters from string for use in variablename

Sun May 10, 2020 12:03 pm

{
local a "Can-t-be-used-as-name-for-variable"
local b "-"
:while condition=[find $a $b] do={
  :set $a ("$[:pick $a 0 ([find $a $b]) ]"."$[:pick $a ([find $a $b]+1) ([:len $a])]")}
:put "Result string: $a"
}     

Result string ($a): Cantbeusedasnameforvariable

The :local variable $b contains the single unwanted character.

I needed that, to be able to store counters in a variable in :global with different names referring to their origin.
:local envName ("varName".$a);
[:parse "global $envName $tempCount"];

Result Global (/system script environment)

name: varNameCantbeusedasnameforvariable value: 10

You can adapt this to other characters and if you want to remove multiple different characters you need to create multiple instances of this script. Or make an other while-do to filter a list of characters.
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Cleaning characters from string for use in variablename

Sun May 10, 2020 3:12 pm

And as an function, my first one :-)
{
 global cleanStringFunc do={
  while condition=[find $1 $2] do={
   set $1 ("$[pick $1 0 ([find $1 $2]) ]"."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
  return $1
 }
put [$cleanStringFunc "Can-t-be-used-as-name-for-variable" "-"]
}
Notice: because of the { at the beginning and at the end a } allows to omit : ; /

To remove other single characters you can do the same call but with a other single character.
put [$cleanStringFunc "Can-t-be-used_as-name-for-variable" "_"]
An variation on it to replace a single character.
{
 global replaceCharacterFunc do={
  while condition=[find $1 $2] do={
   set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
  return $1
 }
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-" "*"]
}
Result: Can*t*be*used*as*name*for*variable

replaceCharacterFunc can replace also the earlier function because you don't have state the replacing character and that can be omitted.
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-" ""]
or
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-"]

Syntax: replaceCharacterFunc <arg> <arg> <arg> [string character-matched replaced-by)
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Cleaning characters from string for use in variablename

Sun May 10, 2020 3:48 pm

The example functions are :global but it seems that I also can use the :local.
{
local cleanStringFunc do={
 while condition=[find $1 $2] do={
  set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
 return $1
}
put [$cleanStringFunc "Can-t-be-used-as-name-for-variable" "-"]
}
Global is handy if you want them also outside the script. You can activate them by calling a other script first.
Put your functions in a script and call it at the start of your scheduled or manual script.
{
if [/system script environment find name=myFuncPresent] do={} else={/system script run myFunc};
# code 
# more code
put [$replaceCharacterFunc "Can-t-be-used-as-name-for-variable" "-"]
}
The myFunc script:
{
global myFuncPresent "Do not manually remove any lines with the wordpart 'Func' in them!!"
} 
{
 global replaceCharacterFunc do={
  while condition=[find $1 $2] do={
   set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
  return $1
 }
}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3445
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Cleaning characters from string for use in variablename

Tue Dec 28, 2021 5:25 am

The myFunc script:
{
global myFuncPresent "Do not manually remove any lines with the wordpart 'Func' in them!!"
} 
{
 global replaceCharacterFunc do={
  while condition=[find $1 $2] do={
   set $1 ("$[pick $1 0 ([find $1 $2]) ]".$3."$[pick $1 ([find $1 $2]+1) ([len $1])]")}
  return $1
 }
}
Very cleaver, thanks!

So cleaver, I stole your function, but added a "char" parameter in return, like:

[$replaceCharacterFunc char="-"]
# or whatever as the value of char
[$replaceCharacterFunc char=":"]
:global replaceCharacterFunc do={
    :local a [tostr $1]
    :local b $char
    :if ([typeof $b]="nil") do={:set b "-"}
    :while ([find $a $b]) do={
        :set $a ("$[:pick $a 0 ([find $a $b]) ]"."$[:pick $a ([find $a $b]+1) ([:len $a])]")}
    :return $a
}

Who is online

Users browsing this forum: No registered users and 27 guests