Re: array function

2015-08-31 Thread cym13 via Digitalmars-d-learn

On Monday, 31 August 2015 at 13:17:30 UTC, cym13 wrote:

On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:
Hey guys, since I am learning D arrays here, can you tell me 
the best way to remove an element at the end of an array or at 
some index i?


import std.algorithm;

T[] arr;
arr = arr.remove(index);

http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays


Also, to remove the last element you can use slices:

T[] arr;
arr = arr[0..$-1];


Re: array function

2015-08-31 Thread cym13 via Digitalmars-d-learn

On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:
Hey guys, since I am learning D arrays here, can you tell me 
the best way to remove an element at the end of an array or at 
some index i?


import std.algorithm;

T[] arr;
arr = arr.remove(index);

http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays


Re: array function

2015-08-31 Thread Namal via Digitalmars-d-learn
Hey guys, since I am learning D arrays here, can you tell me the 
best way to remove an element at the end of an array or at some 
index i?


Re: array function

2015-08-31 Thread Namal via Digitalmars-d-learn

On Monday, 31 August 2015 at 12:00:26 UTC, bearophile wrote:

Namal:


std::vector foo(int N){

std::vector V(N);
int some_array[N];


VLAs are not present in D.

Bye,
bearophile


Yah, I guess I have been damaged with them when I started to 
learn programming in C++ >(


Re: array function

2015-08-31 Thread bearophile via Digitalmars-d-learn

Namal:


std::vector foo(int N){

std::vector V(N);
int some_array[N];


VLAs are not present in D.

Bye,
bearophile


Re: array function

2015-08-31 Thread Namal via Digitalmars-d-learn

On Monday, 31 August 2015 at 11:27:20 UTC, Rikki Cattermole wrote:


You cannot define static arrays using runtime information.
You must use dynamic arrays.

int[] foo(int N) {
int[] v;
v.length = N;
// do something with it
int[] 2;
return s;
}


Of course you can pass it in via a template argument. But this 
of course does not work at runtime like you are wanting.



Hmm, this has never been a problem for me in C++

#include 


std::vector foo(int N){

std::vector V(N);
int some_array[N];

std::vector other_V;

return other_V;

}

int main(){

std::vector V = foo(12);


}

compiles



Re: array function

2015-08-31 Thread Rikki Cattermole via Digitalmars-d-learn

On 31/08/15 11:24 PM, Namal wrote:

Hello,

can someone explain to me please what I am doing wrong by passing an
integer to this function and then just creating a static array? The
error I get is:

Error: variable N cannot be read at compile time

int[] foo(int N){


 int[N] v;
 //do something with it
 int[] s;
 return s;
}

void main(){

 int N = 12;
 int[] A;
 A=prim_numbers(N);

}


You cannot define static arrays using runtime information.
You must use dynamic arrays.

int[] foo(int N) {
int[] v;
v.length = N;
// do something with it
int[] 2;
return s;
}


Of course you can pass it in via a template argument. But this of course 
does not work at runtime like you are wanting.