Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 16:38:34 UTC, Ferhat Kurtulmuş wrote: ş UTF-8: ÅŸ Numeric: ş Ansi: ÅŸ √

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 14:03:54 UTC, DMon wrote: On Wednesday, 21 October 2020 at 13:04:40 UTC, Ferhat Kurtulmuş wrote: import std.range; import std.stdio; void main(){ auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; auto chunks = chunks(source, 2); writeln(chunks[0]); //

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 13:04:40 UTC, Ferhat Kurtulmuş wrote: import std.range; import std.stdio; void main(){ auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; auto chunks = chunks(source, 2); writeln(chunks[0]); // [1, 2] foreach(c; chunks) writeln(c[1]); } An

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 13:43:51 UTC, matheus wrote: foreach (i,j;a){ if(i%2==0){ write(j, ", ");} } Thank you, matheus. for each on the list.

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread matheus via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 12:06:00 UTC, drug wrote: There are two other way: ... // using foreach foreach (i; 0..a.length) write(a[i], ", "); ... Yes you can use foreach, but in this case will not act the way the OP wanted. In his for loop example the "i" is incremented

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 11:55:54 UTC, DMon wrote: What is the simplest way to output every nth element of an array? I can do it using a for loop: void main() { int[5] a = [1,2,3,4,5]; for (int i = 0 ; i <= 4 ; i += 2) { writeln(a[i]); } } Basically, I am wonde

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
On Wednesday, 21 October 2020 at 12:06:00 UTC, drug wrote: There are two other way: Thanks, drug. stride was what I was looking for.

Re: Skipping or Stepping Through an Array?

2020-10-21 Thread drug via Digitalmars-d-learn
There are two other way: ```D import std; void main() { int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]; // using foreach foreach (i; 0..a.length) write(a[i], ", "); writeln; // using stride writeln(stride(a, 2)); } ```

Skipping or Stepping Through an Array?

2020-10-21 Thread DMon via Digitalmars-d-learn
What is the simplest way to output every nth element of an array? I can do it using a for loop: void main() { int[5] a = [1,2,3,4,5]; for (int i = 0 ; i <= 4 ; i += 2) { writeln(a[i]); } } Basically, I am wondering if I missed something.