www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Skipping or Stepping Through an Array?

reply DMon <no mail.com> writes:
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.
Oct 21 2020
next sibling parent reply drug <drug2004 bk.ru> writes:
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));
}
```
Oct 21 2020
next sibling parent DMon <no mail.com> writes:
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.
Oct 21 2020
prev sibling parent reply matheus <matheus gmail.com> writes:
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 by 2: "i+=2". So to perform what OP want with foreach it should be: foreach (i,j;a){ if(i%2==0){ write(j, ", ");} } By the way it's possible to set a "step" value for "foreach"? Matheus.
Oct 21 2020
parent DMon <no mail.com> writes:
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.
Oct 21 2020
prev sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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 wondering if I missed something.
In addition to the drug's solution, this reminds me of the chunks of std.range. 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]); } output: 2 4 6 8 10
Oct 21 2020
parent reply DMon <no mail.com> writes:
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]);
 }
And, thank you Kurtulmuṣ (that's the closest "s" this keyboard has). I've played with std.range but didn't think a control structure or import should be necessary.
Oct 21 2020
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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]); // [1, 2]

     foreach(c; chunks)
     	writeln(c[1]);
 }
And, thank you Kurtulmuṣ (that's the closest "s" this keyboard has). I've played with std.range but didn't think a control structure or import should be necessary.
:) 'sh' sound in English ş Ansi: ÅŸ
Oct 21 2020
parent DMon <no mail.com> writes:
On Wednesday, 21 October 2020 at 16:38:34 UTC, Ferhat Kurtulmuş 
wrote:
 ş



 Ansi: ÅŸ
Oct 21 2020