[Closed] Ignoring % in a format?
Is it possible to have a format “%” to:(stringstream “”)
ignore the %? I have a file format name:
“objname_%04d.obj”
But I don’t want it to replace the % with anything except “%” and I don’t know how many % will be in my stringstream.
Hi Gavin,
maybe you already know, to have the format ignore the % substitution, you must esacape it with “”
format "the \% is equal to %
" 10
-- output: the % is equal to 10
I found also an “ignoreStringEscapes:<boolean>” option related to StringStream values, you can check it. I hope it helps.
- Enrico
escape strings were my first try… but it gets added to like 10 stringstreams. So each time I would have to go through and add a \ in front of each one.
It’s kind of a losing battle. Each time it gets added it’ll lose the \ or return an error. (I presume the ignore escape characters when formatting would be the opposite of what I want wouldn’t it? It would ignore the \ and just return the “%” var. Which would then return an error.)
Where is the ignorestringescapes in the documentation?
EDIT: Ahhhh I see how it works now. That just might work perfectly!
bob = stringstream ""
tim = "[\\\%]( http://forums.cgsociety.org/ )"
format tim to:bob
bob
seek bob 0
filepos bob
(readvalue bob ignorestringEscapes:true)
– Compile error: Illegal input in readValue
– In line: %
Any ideas?
BTW the other system I found was:
format (substitutestring (stringstreamvar as string) “%” “%”) to:bob
that also works.
If you are in Max 2008 or above (or 9 with AVG), you can use ‘@’ before the string to make it a raw string.
But regarding your problem, your error isn’t at all connected to the storage of the string, but readValue. You have a string (% or %) that you are readValue’ing. Type % or % into the listener and see what happens… to get a string, you need to include the quotes. So you need:
tim = "\"\%\""
ignorestringEscapes can be set true or false, doesn’t matter.
Just use the escape character to get a ‘%’ sign as a string, and make sure if you want readValue to return a string, the value itself must be a string… though I don’t know why you’d need to do that but I’m sure there are cases.
Perhaps show us what you are doing and we can see where the problem is, if that still doesn’t work.
Well I have a function:
let’s say
fn bob (
data = stringstream “”
format “C:\ObjName_%04d.obj” to: data
data
)
bigdata = stringstream “”
format bob to:bigdata
tim = createfile @“C: im.txt”
format bigdata to:tim
close tim
And then I do a bunch of other stuff with the ‘bigdata’ stringstream.
So the problem is. The %04d survives the first formatting. But then when the format bob to bigdata line executes it just sees a % and says I don’t have enough arguments (which I don’t.) And then again when the bigdata gets formatted into tim once again it sees the % and returns an insufficient arguments error.
Problem is the format bob to bigdata line equivalent gets executed perhaps hundreds of times. So there are hundreds of unsatisfied %s in the string that are waiting to be filled with variables. (the code is all in the Max <-> Nuke converter posted earlier today.)
So it sounds like using readvalue in front of the function which generates a stringstream would be the solution with the ignoreescapes flag.
The @ isn’t an option because you can’t go:
format @(function as string) to:(stringstream “”) it returns an error. I tried that one too
fn bob =
(
data = stringstream ""
format "C:\\ObjName_\%04d.obj" to: data
data
)
bigdata = stringstream ""
format "%" (bob() as string) to:bigdata --you need 'as string' or else it will format 'Stringstream:'
tim = createfile "C:\ im.txt"
format "%" (bigdata as string) to:tim
close tim
shelllaunch "C:\ im.txt" ""
That works fine for me. I think you are getting confused with what a string literal actually is. String literals don’t actually exist in the string, they just tell the parser how to create a string. So once the string exists, the literal isn’t in the string, since it is already a string, parsed, and in memory, the program knows how to treat it. That, along with using format correctly– you need to give it a string argument of some sort, I just use a % metacharacter, and format the actual string with a percentage sign in it already. Does that make sense?
format() uses the first argument as the formatting string. As Rob said, you have to pass your stringstream as second argument where the first argument is simply “%”.
format “%” whatever to:someoutput
In the above case, whatever can contain as many % as you want without any side effects.