|
|||||||||||||||||||||||||||||||||||||||
Using StringsEvery text variable is a string. Strings are used to format data output, add prompts for commands, and to indicate file paths. Any variable can be converted to a string simply by using new_variable = old_variable as string (You can convert to any variable type using this as method, except you cannot convert a string to a number.) String variables can also be added to an existing string to create a larger string, and to format data output. For instance, suppose you have the variable T = 30 and you want to write a script to output it, just like Temperature = T, but substituting the variable value for T. You can do it this way: “Temperature = “ + T as string which will return Temperature = 30. Outputting String VariablesAll string variables can be output to the Listener. This is useful either to show the result of script calculations, or to prompt the user for input. The PRINT and FORMAT commands are used to output strings. The PRINT command simply prints the string to the Listener, to a new script window, or to a file. For instance, in our script that checks whether its hot or cold, you could output the result using the PRINT command this way: print “It is hot.” For more information on using PRINT to a new script or to a file, see Text File I/O later in this chapter. The FORMAT command does the same as the PRINT command, but its easier to use when printing a series of variables and results. To use the FORMAT command, type the % character in the middle of the string, and it will be replaced by the variable results. For example, in format “If temperature is below % it is cold. Above % it is hot.” cold hot the first % character will be replaced by the value of the cold variable and the second will be replaced with the value of the hot variable. Using Special Characters in String ValuesSome symbols and characters have special meanings when used for string values and must be used in a different format to differentiate them from the actual symbol or character. For instance, in the FORMAT command, the % value represents a variable value that will substitute for it, but if you need to use the actual % symbol, you can use \% to represent it. The next list presents some of the commonly used symbols and characters and their special formatting, and the results youll see when you use that format:
In addition, \\ characters are very important for file path definition.
Manipulating StringsSometimes you might need part of a string, or you might need to search for words and characters inside strings. MAXScript provides you a series of commands that allow you to do it. Searching for Text The FINDSTRING command searches for a string inside another string, and returns the character position of the match. If there is more than one match, it returns only the first. For instance, findstring It is hot here. h will return 7.
Cropping Text The SUBSTRING command crops part of a string and returns it as a new string. It works by defining the character position and the number of characters to copy. For instance, substring It is hot above 80F. 7 3 will return hot. FINDSTRING is very useful when used together with SUBSTRING, to search and filter the string portion you want. For instance, you can use substring “It is hot above 80F.” (findstring “It is hot above 80F.” “h”) 3 This line will search for and copy the letter h and the next two letters, in a single command. Transforming Text into Commands The EXECUTE command executes the string as a command. Its very useful when you need to use an object name in the scene or read some information from a file. For instance, execute temperature = 25.9 would set the value of the variable temperature to 25.9. This value could have been read from a file, or entered as text in any field.
© 2000, Frol (selection, edition, publication) |
|||||||||||||||||||||||||||||||||||||||
|