Using lazily ?

Timon Gehr timon.gehr at gmx.ch
Thu Mar 1 14:10:10 PST 2012


On 03/01/2012 10:50 PM, bioinfornatics wrote:
> dear,
> Noob question for know if D provide a shorter way i explain
> we have a struct S:
>
> struct S{
> 	string member1;
> 	string member2;
> 	string member3;
> }
>
> we parse a file:
> File f = File("a path", "r");
> S s;
> sise_t tokenLength = "member1".length;
> foreach( char[] line; f.byLine() )
>      mixin("s." ~ line[0 .. tokenLength] ~ " = " ~ line[tokenLength ..
> $]" ); // do not work because lien is not kno at compile time
>
>
> I know this do not works i.e comment but it will save some line by
> checking if is member1 2 or 3
>
> They are a shorter way to do this use lazy ?
>

struct S{
     string member1;
     string member2;
     string member3;
}

S s;
size_t tokenLength = "member1".length;
void main(){
     foreach(char[] line; stdin.byLine()) foreach(m;__traits(allMembers,S)){
         if(line[0..tokenLength] == m) mixin("s."~m) = line[tokenLength 
.. $].idup;
     }
}


More information about the Digitalmars-d-learn mailing list