[Closed] How to use variables with verbatim string literals
Anyone know how to make a string value stored in a variable into a verbatim string literal?
For instance, this works:
@“c:\folder\subfolder”
whereas this does not work:
pth=“c:\folder\subfolder”
@pth
It might not be obvious, but the @ verbatim literal does NOT MODIFY a string value, but controls the parser during the evaluation.
(It appears that there is a lot of misunderstanding around this topic, let me know if the explanation below helps so I can format it into the Help)
When you type in a string in the Listener or a MAXScript Editor and press Enter or Ctrl+E, the stream is evaluated to convert to MAXScript expressions and values. When MAXScript encounters a string assignment, the ‘escape sequences’ prefixed with backslash inside the string are normally converted to their corresponding special characters, so “c: emp est.txt” turns into “c:<TAB>emp<TAB>est.txt”. Once again, this ONLY happens when the string is first assigned to a variable and has to go through the process of evaluation.
To avoid this, in the past you had to double your backslashes as “c:\ emp\ est.txt” where the first backslash denotes an ‘escape sequence’, while the second character is the character that is supposed to be used, or use use forward slashes / to avoid the problem altogether.
The verbatim literal tells the parser to DISABLE the ‘escape sequnece’ handling and NOT try to resolve special characters inside the string. Thus @“c: emp est.txt” ends up in memory as is, “c: emp est.txt” without any tabs.
At run time, when the string does NOT go through the parser (for example when read from an external file using getIniSetting, readLine(), readChars etc.), such conversion is NOT performed, so does not needs fixing! Thus calling @pth makes no sense if the content of pth has already passed through the parser – it either has already resolved special characters, or was taken verbatim, but once in memory, it never changes again and can be used as a valid file path in related functions.
There are some special cases related to I/O though – when you save a text file to disk and then read its content back using fileIn() which technically evaluates the content of the file as if you did MAXScript>Run Script…, when calling getValue() on a stringStream to convert the incoming text into MAXScript values, or when calling execute() on a string, you are still evaluating the string. In these cases, the strings go through the parser as usual and would require the same precautions as described above. Thus, if you saved a .MS file using the createFile() and format() methods with the following content:
–start of file
pth = “c: emp est.txt”
–end of file
and try to call fileIn() on this file, you will get the old issue. So you should write out either
–start of file
pth = “c:\ emp\ est.txt”
–end of file
or
–start of file
pth = @“c: emp est.txt”
–end of file
for fileIn() to evaluate the path as “c: emp est.txt” before assigning it to the variable pth.
Once this is done, the variable pth can be used anywhere in your code to open files, call other file-related functions like doesFileExist() or getFileNamePath() etc. without ever needing special \ handling.
Thanks, Bobo
It’s still a bit fuzzy for me, but your explanation made enough sense that I was able to get past my problems.
Thanks.