Thread overview
Multiple %s format specifiers with a single argument
Apr 09, 2012
Andrej Mitrovic
Apr 09, 2012
q66
Apr 09, 2012
Andrej Mitrovic
Apr 09, 2012
Ali Çehreli
Apr 09, 2012
q66
April 09, 2012
import std.string;

void main()
{
    string foo = "foo";
    string bar = format("%s %s %s", foo);
}

format expects 3 arguments, but what I really want is foo to be used for all 3 specifiers and not repeat 'foo' 3 times manually. Are there any format specifiers that do what I want? I've tried using positional specifiers but that didn't work:

string bar = format("%1$s %1$s %1$s", foo);
$ std.format.FormatException@std\format.d(4363): string

That's a great error message btw.
April 09, 2012
On Monday, 9 April 2012 at 17:09:03 UTC, Andrej Mitrovic wrote:
> import std.string;
>
> void main()
> {
>     string foo = "foo";
>     string bar = format("%s %s %s", foo);
> }
>
> format expects 3 arguments, but what I really want is foo to be used
> for all 3 specifiers and not repeat 'foo' 3 times manually. Are there
> any format specifiers that do what I want? I've tried using positional
> specifiers but that didn't work:
>
> string bar = format("%1$s %1$s %1$s", foo);
> $ std.format.FormatException@std\format.d(4363): string
>
> That's a great error message btw.

Positional specifier works just fine for me.
April 09, 2012
On 4/9/12, q66 <quaker66@gmail.com> wrote:
> Positional specifier works just fine for me.

Which version are you using? I'm on 2.058.
April 09, 2012
On 04/09/2012 10:24 AM, Andrej Mitrovic wrote:
> On 4/9/12, q66<quaker66@gmail.com>  wrote:
>> Positional specifier works just fine for me.
>
> Which version are you using? I'm on 2.058.

Positional parameters[*] are supported in 2.058. This example prints the same argument in decimal, hexadecimal, octal, and binary:

    writefln("%1$d %1$x %1$o %1$b", 42);

Ali

[*] http://ddili.org/ders/d.en/formatted_output.html
April 09, 2012
On Monday, 9 April 2012 at 17:24:35 UTC, Andrej Mitrovic wrote:
> On 4/9/12, q66 <quaker66@gmail.com> wrote:
>> Positional specifier works just fine for me.
>
> Which version are you using? I'm on 2.058.

git