Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
October 21, 2020 Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
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. |
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DMon | 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)); } ``` |
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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.
|
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DMon | 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
|
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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.
|
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to matheus | 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.
|
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ferhat Kurtulmuş | 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.
|
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DMon | 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
ş
UTF-8: ÅŸ
Numeric: ş
Ansi: ÅŸ
|
October 21, 2020 Re: Skipping or Stepping Through an Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ferhat Kurtulmuş | On Wednesday, 21 October 2020 at 16:38:34 UTC, Ferhat Kurtulmuş wrote:
> ş
>
> UTF-8: ÅŸ
> Numeric: ş
> Ansi: ÅŸ
√
|
Copyright © 1999-2021 by the D Language Foundation