Thread overview
does std.file.slurp work with string data field?
Dec 27, 2013
Big Tummy
Dec 27, 2013
bearophile
Dec 27, 2013
bearophile
Dec 27, 2013
Big Tummy
Dec 27, 2013
Ali Çehreli
December 27, 2013
I have a file containing data like,

--- data.txt ---
abc, 1
def, 2
--- end data.txt ---

I try to read in the data like following, but the the codes do not compile:

--- mytest.d ---
import std.file, std.stdio;
void main() {
    auto arr = "data.txt".slurp!(string, int)("%s, %s");
    foreach(a; arr) {
        writefln("%s: %d", a[0], a[1]);
    }
}
--- end mytest.d ---

Any idea?

Thanks in advance,
Big Tummy
December 27, 2013
Big Tummy:

>     auto arr = "data.txt".slurp!(string, int)("%s, %s");

Use (I also have added a space after the second %s because slurp is buggy with Windows-style newlines):

auto arr = slurp!(string, int)("data.txt", "%s, %s ");

Bye,
bearophile
December 27, 2013
> auto arr = slurp!(string, int)("data.txt", "%s, %s ");

Sorry, ignore this because it's essentially the same code as yours. What's the error you are receiving? (And better to ask such questions in D.learn).

Bye,
bearophile
December 27, 2013
On Friday, 27 December 2013 at 02:05:42 UTC, bearophile wrote:
> Big Tummy:
>
>>    auto arr = "data.txt".slurp!(string, int)("%s, %s");
>
> Use (I also have added a space after the second %s because slurp is buggy with Windows-style newlines):
>
> auto arr = slurp!(string, int)("data.txt", "%s, %s ");
>
> Bye,
> bearophile

Not working for me. It seems to have something to do with string not being swappable:

> rdmd mytest.d
core.exception.AssertError@std.algorithm(1942): Assertion failure
----------------
/tmp/.rdmd-1001/rdmd-my2.d-AEA8686B37E3195901320EEC6C7A9E25/my2(_d_assertm+0x16) [0x80b0766]
/tmp/.rdmd-1001/rdmd-my2.d-AEA8686B37E3195901320EEC6C7A9E25/my2() [0x80b4a3a]
/tmp/.rdmd-1001/rdmd-my2.d-AEA8686B37E3195901320EEC6C7A9E25/my2(pure nothrow @trusted void std.algorithm.swap!(std.typecons.Tuple!(immutable(char)[], int).Tuple).swap(ref std.typecons.Tuple!(immutable(char)[], int).Tuple, ref std.typecons.Tuple!(immutable(char)[], int).Tuple)+0x59) [0x80aae01]

December 27, 2013
On 12/26/2013 06:16 PM, Big Tummy wrote:
> On Friday, 27 December 2013 at 02:05:42 UTC, bearophile wrote:
>> Big Tummy:
>>
>>>    auto arr = "data.txt".slurp!(string, int)("%s, %s");
>>
>> Use (I also have added a space after the second %s because slurp is
>> buggy with Windows-style newlines):
>>
>> auto arr = slurp!(string, int)("data.txt", "%s, %s ");
>>
>> Bye,
>> bearophile
>
> Not working for me. It seems to have something to do with string not
> being swappable:
>
>> rdmd mytest.d
> core.exception.AssertError@std.algorithm(1942): Assertion failure
> ----------------
> /tmp/.rdmd-1001/rdmd-my2.d-AEA8686B37E3195901320EEC6C7A9E25/my2(_d_assertm+0x16)
> [0x80b0766]
> /tmp/.rdmd-1001/rdmd-my2.d-AEA8686B37E3195901320EEC6C7A9E25/my2()
> [0x80b4a3a]
> /tmp/.rdmd-1001/rdmd-my2.d-AEA8686B37E3195901320EEC6C7A9E25/my2(pure
> nothrow @trusted void
> std.algorithm.swap!(std.typecons.Tuple!(immutable(char)[],
> int).Tuple).swap(ref std.typecons.Tuple!(immutable(char)[], int).Tuple,
> ref std.typecons.Tuple!(immutable(char)[], int).Tuple)+0x59) [0x80aae01]
>

dmd git head compiles your code. Use the cutting edge dmd or please wait for the next release. :)

Ali