www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - does std.file.slurp work with string data field?

reply "Big Tummy" <bigtummy gmail.com> writes:
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
Dec 26 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Dec 26 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
 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
Dec 26 2013
prev sibling parent reply "Big Tummy" <bigtummy gmail.com> writes:
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/ y2(_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]
Dec 26 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Dec 26 2013