Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

printf and %{}

Aug
1,892
66
%{} is a shortcut for %@execstr

This works;
Code:
E:\Utils>printf "2023 subtract 1947 is %i\n"  %@execstr[expr 2023-1947]
2023 subtract 1947 is 76
...but this does not...
Code:
E:\Utils>printf "2023 subtract 1947 is %i\n"  %{expr 2023-1947}
Usage : EXPR expression
2023 subtract 1947 is
All by itself, it does return the correct answer;
Code:
E:\Utils>set result=%{expr 2023-1947} 

E:\Utils>echo %result
76

This also works;
Code:
E:\Utils>echo 2023 subtract 1947 is %{expr 2023-1947}
2023 subtract 1947 is 76

Why will %{} not work with printf?

I am aware that I could also use;
Code:
E:\Utils>printf "2023 subtract 1947 is %i\n"  %@eval[2023-1947] 
2023 subtract 1947 is 76
...but the issue is the use of %{} with printf.

Joe
 
Code:
printf "2023 subtract 1947 is %i\n" "%{expr 2023-1947}"
2023 subtract 1947 is "76"

does work ...

Also this ...
Code:
printf "2023 subtract 1947 is %i\n" (%{expr 2023-1947})
2023 subtract 1947 is (76)

and this ...
Code:
printf "2023 subtract 1947 is %i\n" [%{expr 2023-1947}]
2023 subtract 1947 is [76]

And even a weird thing like the following "works" ...
Code:
printf "2023 subtract 1947 is %i\n" "%{expr 2023-1947}
2023 subtract 1947 is "76
 
Last edited:
Thanks for that.

Hoping @rconn can explain why %{expr 2023-1947} has to be quoted,
when used with printf.

Joe
 
And in the help it says

Code:
%[flags][width][.precision][length]type

but it doesn't explain "length". Should that be

Code:
%[flags][width][.precision][L]type

The help does explain "L".
 
%{} is a shortcut for %@execstr

This works;
Code:
E:\Utils>printf "2023 subtract 1947 is %i\n"  %@execstr[expr 2023-1947]
2023 subtract 1947 is 76
...but this does not...
Code:
E:\Utils>printf "2023 subtract 1947 is %i\n"  %{expr 2023-1947}
Usage : EXPR expression
2023 subtract 1947 is
All by itself, it does return the correct answer;
Code:
[/QUOTE]

The problem is with the white space in the argument {expr 2023-1947}.

PRINTF sees the %i argument in the format string, so it looks for the *first* argument in the argument string, which is "%{expr". If you double quote the expr expression, it will be seen as a single argument.
 
Back
Top