digitalmars.D.learn - foreach loop
- Namal (9/9) Oct 19 2015 Is it possible to create a foreach loop with a breakstetemen?
- Dominikus Dittes Scherkl (2/11) Oct 19 2015 Of course. D uses even the same keyword for this as C does: break.
- Daniel Kozak via Digitalmars-d-learn (13/25) Oct 19 2015 V Mon, 19 Oct 2015 14:28:03 +0000
- Rikki Cattermole (12/21) Oct 19 2015 If you want to break a specific loop, use labels.
- Namal (3/28) Oct 19 2015 Hey thanks, until now I just used bool variables as break flags.
- Namal (4/4) Oct 19 2015 Is it possible to use foreach backwards?
- novice2 (4/5) Oct 19 2015 yes
- Mike Parker (9/13) Oct 19 2015 foreach_reverse(i; 1 .. 20)
- Namal (4/4) Nov 03 2015 Hello guys,
- wobbles (5/9) Nov 03 2015 auto arr = ["Hello", "World"];
- Adam D. Ruppe (3/6) Nov 03 2015 for many of them it is as simple as:
- Namal (10/16) Nov 03 2015 Thank you. I am still struggling with the functional ways of D.
- wobbles (6/25) Nov 03 2015 writefln("Count is: %s", arr
- wobbles (3/30) Nov 03 2015 Oh, I realise now you were counting the number of 'false' values.
- Namal (7/36) Nov 03 2015 How do I save sum as integer or something I try;
- Namal (4/33) Nov 03 2015 well I tried this that way, but my count stays 0, same as if I do
- Edwin van Leeuwen (7/13) Nov 03 2015 Shouldn't you be using walkLength instead of sum, since you are
- wobbles (5/19) Nov 03 2015 That would work also yes.
- Edwin van Leeuwen (5/26) Nov 03 2015 false converts to zero, so
- TheFlyingFiddle (4/7) Nov 03 2015 You could also use count:
- ixid (11/12) Nov 04 2015 reduce!((x, y) => x + !y)(0, arr).writeln;
Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; }
Oct 19 2015
On Monday, 19 October 2015 at 14:28:06 UTC, Namal wrote:Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; }Of course. D uses even the same keyword for this as C does: break.
Oct 19 2015
V Mon, 19 Oct 2015 14:28:03 +0000 Namal via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; }I am not sure what you exactly want, but you can break foreach statement: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; outerfor: foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; if (iWantToBreak) break outerfor; }
Oct 19 2015
On 20/10/15 3:28 AM, Namal wrote:Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; }If you want to break a specific loop, use labels. L1: foreach(i; 0 .. 10) { L2: foreach(j; 0 .. 10) { // break L1; // break L2; // or // break; } }
Oct 19 2015
On Monday, 19 October 2015 at 14:43:04 UTC, Rikki Cattermole wrote:On 20/10/15 3:28 AM, Namal wrote:Hey thanks, until now I just used bool variables as break flags.Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; }If you want to break a specific loop, use labels. L1: foreach(i; 0 .. 10) { L2: foreach(j; 0 .. 10) { // break L1; // break L2; // or // break; } }
Oct 19 2015
Is it possible to use foreach backwards? foreach(int i;20..1) writeln(i); compiles but I get nothing.
Oct 19 2015
On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote:Is it possible to use foreach backwards?yes http://dlang.org/statement.html#ForeachStatement http://dpaste.dzfl.pl/cf847a9e1595
Oct 19 2015
On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote:Is it possible to use foreach backwards? foreach(int i;20..1) writeln(i); compiles but I get nothing.foreach_reverse(i; 1 .. 20) writeln(i); Or: import std.range : iota, retro; foreach(i; iota(1, 20).retro) writeln(i); But if you really want the numbers 1 to 20, you should use 21 as the end value in both cases, as it's exclusive.
Oct 19 2015
Hello guys, I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.
Nov 03 2015
On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:Hello guys, I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.auto arr = ["Hello", "World"]; foreach(int idx, string str; arr){ writefln("%s = %s", idx, str); }
Nov 03 2015
On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.for many of them it is as simple as: foreach(index, element; array) { }
Nov 03 2015
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool[] arr = [ture, false, ture, ...]; int count; foreach(i;arr){ if(!i) count++; } writeln(count);I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.for many of them it is as simple as: foreach(index, element; array) { }
Nov 03 2015
On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool[] arr = [ture, false, ture, ...]; int count; foreach(i;arr){ if(!i) count++; } writeln(count);I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.for many of them it is as simple as: foreach(index, element; array) { }
Nov 03 2015
On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:Oh, I realise now you were counting the number of 'false' values. I counted the true values - so the filter line is wrong here.On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool[] arr = [ture, false, ture, ...]; int count; foreach(i;arr){ if(!i) count++; } writeln(count);[...]for many of them it is as simple as: foreach(index, element; array) { }
Nov 03 2015
On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:How do I save sum as integer or something I try; arr.writeln; return arr.filter!(a=>a==false).sum; but I get [true, false, true, false, true, true, true, false, true, false] 0On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool[] arr = [ture, false, ture, ...]; int count; foreach(i;arr){ if(!i) count++; } writeln(count);I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.for many of them it is as simple as: foreach(index, element; array) { }
Nov 03 2015
On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:well I tried this that way, but my count stays 0, same as if I do it in an int function with a return though I clearly have some false elements in the arr.On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote:writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool[] arr = [ture, false, ture, ...]; int count; foreach(i;arr){ if(!i) count++; } writeln(count);I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.for many of them it is as simple as: foreach(index, element; array) { }
Nov 03 2015
On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:Shouldn't you be using walkLength instead of sum, since you are counting the left over values? import std.range : walkLength; writefln("Count is: %s", arr .filter!(a => a==false) .walkLength);writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);
Nov 03 2015
On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen wrote:On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:That would work also yes. Be interesting to know which is more efficient actually - I suspect they're very similar.Shouldn't you be using walkLength instead of sum, since you are counting the left over values? import std.range : walkLength; writefln("Count is: %s", arr .filter!(a => a==false) .walkLength);writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);
Nov 03 2015
On Tuesday, 3 November 2015 at 16:55:44 UTC, wobbles wrote:On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen wrote:false converts to zero, so [false,false,false].sum == 0 Of course true converts to one -> [true,true,true].sum == 3On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:That would work also yes. Be interesting to know which is more efficient actually - I suspect they're very similar.Shouldn't you be using walkLength instead of sum, since you are counting the left over values? import std.range : walkLength; writefln("Count is: %s", arr .filter!(a => a==false) .walkLength);writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b);
Nov 03 2015
On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote:well I tried this that way, but my count stays 0, same as if I do it in an int function with a return though I clearly have some false elements in the arr.You could also use count: http://dlang.org/phobos/std_algorithm_searching.html#count return arr.count!(x => !x);
Nov 03 2015
On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:reduce!((x, y) => x + !y)(0, arr).writeln; This would probably be the preferred way, that uses a lambda function (x, y) => x + !y which adds the inverse of the next array value (y) to the total so far (x). You have to provide 0 as the first argument to reduce as it is the seed, otherwise it will use the first value in the array as the seed and convert it to an int, making the total 1 too high as the first value is 'true'. You can also use a string but this is frowned on style-wise though in this case it is clearer: reduce!"a + !b"(0, arr).writeln;Can you help me out please. Thx.
Nov 04 2015