Avoiding "Error 66 - Invalid variable identifier ..."

Continuing the discussion from AmiBroker 6.26.0 BETA released:

It’s been a while since I’ve seriously looked at AmiBroker, and was searching the forum recently for help on another issue, when I came across the thread AmiBroker 6.26.0 BETA released, where there was a lot of discussion about the new way of declaring/ using static variables. I learnt a lot from the discussion, and hopefully @Tomasz will make the feature public, once people have had a bit more time to digested and understand it.

Anyway, the purpose of this thread is in relation to something else that was touched-on - unintended coding errors in relation to dynamic variables when using StaticVarSet(), particularly when using the string result produced by the Name() function, to concatenate with other string/s, to create a unique/ symbol-specific variable, eg.

StaticVarSet(“myStatic” + Name(), someValueOrVariable)

If the string of characters produced by Name() contains invalid characters (for variable naming purposes), then the result is probably going to be Error 66 …, clearly illustrated by @fxshrat here.

Most programming languages have similar rules to AmiBroker for naming of variables – the following comes from “C++ Language Tutorial”, Jun/2007, by Juan Soulié, available here:

A valid identifier is a sequence of one or more letters, digits or underscore characters (). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character ( ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case they can begin with a digit.

, and:

Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are reserved keywords. The standard reserved keywords are: ...

In relation to the question, why do we need such rules?, the answer is simple, they make it clear to the compiler/ interpreter which bits of the text file/ script/ program represent a mathematical or logical operation, and which should be used as a container for data. Clarified by @Tomasz here.

One way to avoid them in the first place, is to strip-out anything that doesn’t conform, before passing the string to VarSet(), eg. the following simple function replaces unacceptable characters with more suitable ones:

	function StripSpecialChars(inpString)
Description: Remove special characters from a string
	NewStr	= StrReplace(inpString,"$","D") ; 
	NewStr	= StrReplace(NewStr,"&","_") ;
	NewStr	= StrReplace(NewStr,"^","_") ;
	NewStr	= StrReplace(NewStr,"£","P") ;

	return NewStr ;

There are many other illegal characters. Anything that is not 'a'..'z', 'A'..'Z', '0'...'9' and underscore should not be used in variable identifiers. For example punctuation -, +, ., ,, :, / should not be used in identifiers.

A regular expression function would probably handle it with ease, but I don't think we have one of them in the AB toolkit, also, probably not a priority at this time.