digitalmars.D.learn - Skipping or Stepping Through an Array?
- DMon (11/11) Oct 21 2020 What is the simplest way to output every nth element of an array?
- drug (14/14) Oct 21 2020 There are two other way:
- DMon (3/4) Oct 21 2020 Thanks, drug.
- matheus (10/16) Oct 21 2020 Yes you can use foreach, but in this case will not act the way
- DMon (2/5) Oct 21 2020 Thank you, matheus. for each on the list.
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (18/30) Oct 21 2020 In addition to the drug's solution, this reminds me of the chunks
- DMon (6/15) Oct 21 2020 And, thank you Kurtulmuṣ (that's the closest "s" this keyboard
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (6/24) Oct 21 2020 :) 'sh' sound in English
- DMon (3/7) Oct 21 2020 √
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
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
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
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
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
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
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
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::) 'sh' sound in English ş Ansi: ÅŸ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
On Wednesday, 21 October 2020 at 16:38:34 UTC, Ferhat Kurtulmuş wrote:ş Ansi: ÅŸ√
Oct 21 2020









DMon <no mail.com> 