format() function and positional specifiers

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

format() function and positional specifiers

Post by n.allan » Sun May 24, 2020 5:55 pm

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?

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Re: format() function and positional specifiers

Post by n.allan » Sun May 31, 2020 8:50 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: format() function and positional specifiers

Post by dunbarx » Sun May 31, 2020 1:41 pm

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

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Re: format() function and positional specifiers

Post by n.allan » Sun May 31, 2020 2:53 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: format() function and positional specifiers

Post by dunbarx » Mon Jun 01, 2020 12:17 am

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: format() function and positional specifiers

Post by mwieder » Tue Jun 02, 2020 4:40 am

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.

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Re: format() function and positional specifiers

Post by n.allan » Thu Jun 04, 2020 3:30 pm

I will file an enhancement request.

Thanks everyone.

Post Reply