www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How I can iterate data in structure

reply "Suliman" <evermind live.ru> writes:
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;

}
Aug 22 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 22 2014
parent reply "Suliman" <evermind live.ru> writes:
foreach (field; result.tupleof)
Why I should here specify type of iterable element, but not first element that I use for iteration? I mean: foreach (_some_type_possible_enum_ field; result) ?
Aug 22 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Suliman:

foreach (field; result.tupleof)
Why I should here specify type of iterable element, but not first element that I use for iteration? I mean: foreach (_some_type_possible_enum_ field; result) ?
I don't understand your question. In my code I have not specified types in the foreach. Bye, bearophile
Aug 22 2014
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 22 August 2014 at 08:44:51 UTC, Suliman wrote:
foreach (field; result.tupleof)
Why I should here specify type of iterable element, but not first element that I use for iteration? I mean: foreach (_some_type_possible_enum_ field; result) ?
You mustn't, because your struct could have fields of different types. When you `foreach()` over a tuple, the compiler unrolls the loop body, which allows it to use a (potentially) different type on each iteration. If you don't want this, and all the fields have the same type, you can iterate over an array made from the fields: foreach (field; [result.tupleof]) { writeln(field); }
Aug 22 2014
parent "Gary Willoughby" <dev nomad.so> writes:
On Friday, 22 August 2014 at 10:44:31 UTC, Marc Sch├╝tz wrote:
 On Friday, 22 August 2014 at 08:44:51 UTC, Suliman wrote:
foreach (field; result.tupleof)
Why I should here specify type of iterable element, but not first element that I use for iteration? I mean: foreach (_some_type_possible_enum_ field; result) ?
You mustn't, because your struct could have fields of different types. When you `foreach()` over a tuple, the compiler unrolls the loop body, which allows it to use a (potentially) different type on each iteration. If you don't want this, and all the fields have the same type, you can iterate over an array made from the fields: foreach (field; [result.tupleof]) { writeln(field); }
Or you could implement opApply or range primitives in the struct. http://ddili.org/ders/d.en/foreach_opapply.html http://dlang.org/phobos/std_range.html
Aug 22 2014