string format

Alex Tweedly alex at tweedly.net
Sun Aug 28 12:47:43 EDT 2005


Charles Hartman wrote:

>
> On Aug 28, 2005, at 5:01 AM, Alex Tweedly wrote:
>
>>>> . . . the particular problem of adding spaces after  a string to  
>>>> fill it out to a specified length.
>>>
>>>
>>> It would work if the "incantation" argument of format() would  
>>> accept  a variable name, but I can't find a way to make it do  that 
>>> . . .
>>
>>
>> You can't do      format("%<myVar>d", otherVar).
>>
>> But you can do
>>  put "%" & myVar & "d" into temp
>>  format(temp, otherVar)
>
>
> I'm not sure you can. 

I *am* sure I can - I tried it before I sent the email :-)

> This in the message box
>
>     put "string" into s; put "%" & (20 - length(s)) & "d"into tTmp;  
> put format(tTmp, s)
>
> produces
>
>    -1879024420
>
Right. You asked it to print s (which holds "string" as a 14 digit 
decimal number - could have got almost anything).
The format string you wanted was "%14s"

> (with a total length of 14), though 'put tTmp' produces '%14' as  
> expected. Did I miss something?
>
> Anyway, the solution in this direction that I was thinking of is  
> presumably too baroque to be useful -- something like
>
>     put " " into s
>     put "%" & 20 - length(s) & "d" into tTailFmt
>     put myString & format(tTailFmt, s)
>
if you made it be
    put " " into s
    put "%" & 20 - length(s) & "s" into tTailFmt      -- note the "s" 
not "d"
    put myString & format(tTailFmt, s)

Then it will work (kind of).

It is a bit baroque - but a more serious problem is what it does in the 
case where myString is already longer than the desired length. The 
earlier solutions either left it unchanged (sounds good) or truncated to 
exactly the the desired length (also sounds good). This will *always* 
append at least one space, which doesn't sound so good to me.

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.10.16/83 - Release Date: 26/08/2005




More information about the use-livecode mailing list