Page 1 of 1
format() function and positional specifiers
Posted: Sun May 24, 2020 5:55 pm
by n.allan
I see the dictionary in 9.6.0.-rc-1, the entry for the format() function says that the format string is the same as the cpp printf() function.
On "some" cpp printf() web pages there can be positional specifiers in the format string. You can pass the parameters in a certain order, then they will be be evaluated in a different order.
Code: Select all
on testFormat
local tFormat, tPosFormat, tA, tB, tC
put "%s,%s,%s" into tFormat -- standard format string
put "%3$s,%2$s,%1$s" into tPosFormat -- format string with 3$,2$,1$ positional specifiers
put "A" into tA -- some test params
put "B" into tB
put "C" into tC
answer format(tFormat,tA,tB, tC) -- msg box with "A,B,C"
answer format(tPosFormat, tA, tB, tC) -- throws error (I was expecting a msg box with "C,B,A")
end testFormat
Am I getting my format string wrong or are the positional parameters not supported by LiveCode?
Re: format() function and positional specifiers
Posted: Sun May 31, 2020 8:50 am
by n.allan
I assume the positional specifiers are only supported in special cases of printf(), thus not supported in LiveCode.
My interim solution will be to use some kind of conditional statement prior to calling the format() function. Something like this...
Code: Select all
on testFormat pOrder -- pass in an "order" string CBA, BCA etc...
local tFormat, tA, tB, tC
put "%s,%s,%s" into tFormat -- standard format string
put "A" into tA -- some test params
put "B" into tB
put "C" into tC
switch pOrder
case "CBA"
answer format(tFormat, tC, tB, tA) -- CBA Order
break
case "BAC"
answer format(tFormat, tB, tA, tC) -- CBA Order
break
default
answer format(tFormat, tA, tB, tC) -- default order is ABC
end switch
end testFormat
I realise I could perform some kind of string manipulation on the format() string prior to calling the format() function but this will be sufficient for my needs.
Re: format() function and positional specifiers
Posted: Sun May 31, 2020 1:41 pm
by dunbarx
Hi.
I have never heard of anything in LC where one can intrinsically rearrange the order of passed parameters. You can certainly change the "order" under script control by explicitly reassigning the contents of variables, sort of as you posted.
I am not familiar with the advantages of dong so. What are they? And what are you trying to achieve?
Craig
Re: format() function and positional specifiers
Posted: Sun May 31, 2020 2:53 pm
by n.allan
This website for printf() with positional specifiers has a good general case scenario where some spoken languages order their words differently. The positional specifiers could be used as part of a translation tool or some such. See here...
https://www.gnu.org/software/gawk/manua ... ering.html
In my own case, I am parsing huge files of latitude, longitude, height data and creating comma separated files. The target software can expect the data different order. Lat,Lon,Height<crlf> for some packages and Lon,Lat,Height<crlf> for other packages.
My idea was to pre build all the format strings with positional specifiers and simply call the same format () function with the pre-built strings along with the parameters in the same order.
It's no big deal for me, it could have been a minor performance improvement. As you probably know very well, we are always looking to squeeze out a little extra speed out of our scripts but over the years I have learned to deal with it.
Re: format() function and positional specifiers
Posted: Mon Jun 01, 2020 12:17 am
by dunbarx
I see now. OK.
But I would just have made some custom properties that held the various required formats, and used them as "templates" to order the ,er, order of parameters.
Craig
Re: format() function and positional specifiers
Posted: Tue Jun 02, 2020 4:40 am
by mwieder
Neil-
I don't *think* LiveCode's format statement supports positional modifiers, but it looks like you've empirically proved it doesn't. That would definitely be worth filing an enhancement request for though.
Re: format() function and positional specifiers
Posted: Thu Jun 04, 2020 3:30 pm
by n.allan
I will file an enhancement request.
Thanks everyone.