Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape special char sequence in string
#1
Hi,

I'm trying to create a simple utility macro to receive a list of variable names and incorporate them into formatted lines for output/logging, like so :

Code:
Copy      Help
out "var1 = %s" var1

..so I can just copy and paste the QM output into a macro.

I have been unsuccessful in my attempts to escape the char sequence "%s" so it shows up as is in the QM output. In other words, I need a plain text version of "%s" (i.e. not processed) to appear where indicated in the below macro.


Macro
Code:
Copy      Help
str outform
if(!inp(outform "" "" "[]")) ret
ARRAY(str) a=outform
int i
for(i 0 a.len) out "out ''%s = <need to insert escaped percent+s here>'' %s" a[i] a[i]

Thanks,

S
#2
I think what you are looking to do is this...
_s.format("out %s = <need to insert escaped percent+s here>'" a[i]
out _s
#3
Thanks VIg, but either I don't understand how that will help or I didn't explain my problem very well...

If you run the short macro I've pasted above and enter a word in the input box (in this example I used "var1"), the text written to the QM output window would look like this:

out "var1 = <need to insert escaped percent+s here>" var1

This is close to what I need, except instead of the "<need to insert escaped percent+s here>" part, I actually want the characters "%s". The "<need to...>" part is there just to show where I need help.

In other words, if it was done correctly, upon running the macro and entering the word "var1" in the input box, the text printed to the QM output window should look exactly like this:

out "var1 = %s" var1

Is that any clearer?

Thanks!
#4
This works...

Function Function16
Code:
Copy      Help
str outform
if(!inp(outform "" "" "[]")) ret
ARRAY(str) a=outform
int i
for(i 0 a.len)
,_s.format("out [34]%s = _s[34] %s" a[i] a[i])
,_s.findreplace("_" "%")
,out _s

I'm not sure why .format doesn't just let [37] be a % without using it as part of the function...it caught me up for a bit. Find replace will do the trick...you can use more complicated combinations of characters to make sure it replaces what it is supposed to.
#5
out "%%s"
#6
Function Function16
Code:
Copy      Help
str outform
if(!inp(outform "" "" "[]")) ret
ARRAY(str) a=outform
int i
for(i 0 a.len)
,_s.format("out [34]%s = %%s[34] %s" a[i] a[i])
,out _s

Der...in the help file!
#7
Thanks!
#8
Quote:Der...in the help file!

Where, please? Damned if I didn't spend over an hour trying everything I could find that seemed relevant on escaping, char codes. That''s been my major problem - I know what I want but don't always know enough terminology to know where to find it.
#9
Look under "VARIANT members table", is this what you wanted?
Taking on Quick Macros one day at a time
#10
As if I wasn't feeling low enough because of how little I know, along comes the VARIANT members table : (

Actually I was wondering where it is explained that "%%s" is an escape for %s (after having really tried to figure that one out myself for quite a while...)

But, as a wise forum member says, "...one day at a time."
#11
%% for % is briefly mentioned in Format String of help

Quote:Tips
The mostly used format fields are %i for integers and %s for strings.
To include variables in format fields, at first format the format-string with these variables. Use %% for %.
To insert this function, can be used the Text dialog from the floating toolbar.


Forum Jump:


Users browsing this thread: 1 Guest(s)