How I can iterate data in structure

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 22 01:19:31 PDT 2014


Suliman:

> void main()
> {
> 	
> 	auto result = readconfig();
>
> 	foreach (_; result)
> 	{
> 		// I want to iterate result that I got from structure.
> 	}
> }
>
> auto readconfig()
> {
> 	struct ConfigStruct
> 	{
> 		string key1;
> 		string key2;
> 	}
>
> 	ConfigStruct confstruct = ConfigStruct();
> 	confstruct.key1="Ivan";
> 	confstruct.key2="admin";
>
> 	return confstruct;
>
> }

Be careful to make ConfigStruct a static struct.

auto readconfig() {
     static struct ConfigStruct {
         string key1, key2;
     }

     return ConfigStruct("Ivan", "admin");
}

void main() {
     import std.stdio;
     auto result = readconfig();

     foreach (field; result.tupleof) {
         writeln(field);
     }
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list