Dynamic array and foreach loop
Binarydepth via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 9 08:37:22 PDT 2015
On Sunday, 9 August 2015 at 00:22:53 UTC, Jay Norwood wrote:
> On Saturday, 8 August 2015 at 18:28:25 UTC, Binarydepth wrote:
>>> This is the new code :
>>>
>>> foreach(num; 0..liEle) {//Data input loop
>>>
>>> write("Input the element : ", num+1, " ");
>>> readf(" %d", &liaOrig[num]);
>>> }
>>
>> Even better :
>>
>> foreach(num; 0..liaOrig.length
>
> I believe they usually do something like:
>
> foreach( num, ref elem; liaOrig){
>
> }
>
> which creates the index num and the reference to the element of
> range liaOrig.
>
> It also seems that a lot of discussion is going on about
> reducing use of foreach loops in their preferred style, so you
> might want to try some of that.
So I should use the REF like this ?
import std.stdio : writeln;
void main() {
immutable a=5;
int[a] Arr;
foreach(num; 0..a) {
Arr[num] = num;
}
foreach(num, ref ele; Arr) {
writeln(Arr[ele]+1);//Using the REF
}
}
More information about the Digitalmars-d-learn
mailing list