Cleaning characters from string for use in variablename

{
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.

And as an function, my first one :slight_smile:

{
 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: Cantbeusedasnamefor*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 [string character-matched replaced-by)

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
 }
}

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
}