www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach loop

reply Namal <sotis22 mail.ru> writes:
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
next sibling parent Dominikus Dittes Scherkl <Dominikus.Scherkl continental-corporation.com> writes:
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
prev sibling next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
prev sibling next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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
parent reply Namal <sotis22 mail.ru> writes:
On Monday, 19 October 2015 at 14:43:04 UTC, Rikki Cattermole 
wrote:
 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; } }
Hey thanks, until now I just used bool variables as break flags.
Oct 19 2015
parent reply Namal <sotis22 mail.ru> writes:
Is it possible to use foreach backwards?

foreach(int i;20..1)
  writeln(i);

compiles but I get nothing.
Oct 19 2015
next sibling parent novice2 <sorry noem.ail> writes:
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
prev sibling parent Mike Parker <aldacron gmail.com> writes:
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
prev sibling parent reply Namal <sotis22 mail.ru> writes:
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
next sibling parent wobbles <grogan.colin gmail.com> writes:
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
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent reply Namal <sotis22 mail.ru> writes:
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:
 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) { }
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);
Nov 03 2015
next sibling parent reply wobbles <grogan.colin gmail.com> writes:
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:
 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) { }
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);
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
next sibling parent wobbles <grogan.colin gmail.com> writes:
On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:
 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:
 On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
 [...]
for many of them it is as simple as: foreach(index, element; array) { }
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);
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);
Oh, I realise now you were counting the number of 'false' values. I counted the true values - so the filter line is wrong here.
Nov 03 2015
prev sibling next sibling parent Namal <sotis22 mail.ru> writes:
On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:
 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:
 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) { }
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);
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);
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] 0
Nov 03 2015
prev sibling parent reply Namal <sotis22 mail.ru> writes:
On Tuesday, 3 November 2015 at 15:10:43 UTC, wobbles wrote:
 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:
 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) { }
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);
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);
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.
Nov 03 2015
next sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal 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);
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);
Nov 03 2015
parent reply wobbles <grogan.colin gmail.com> writes:
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:
 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);
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);
That would work also yes. Be interesting to know which is more efficient actually - I suspect they're very similar.
Nov 03 2015
parent Edwin van Leeuwen <edder tkwsping.nl> writes:
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:
 On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal 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);
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);
That would work also yes. Be interesting to know which is more efficient actually - I suspect they're very similar.
false converts to zero, so [false,false,false].sum == 0 Of course true converts to one -> [true,true,true].sum == 3
Nov 03 2015
prev sibling parent TheFlyingFiddle <borin.lukas gmail.com> writes:
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
prev sibling parent ixid <adamsibson hotmail.com> writes:
On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote:
Can you help me out please. Thx.
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;
Nov 04 2015