On Friday, 4 September 2015 at 12:09:19 UTC, Edwin van Leeuwen
wrote:
On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen
wrote:
On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote:
import std.algorithm, std.range, std.array, std.string,
std.stdio,
std.conv;
int[] arr1 = [1, 2, 30];
//arr1.max.writeln; // Doesn't work, as you say
arr1.reduce!max.writeln; // This does. Prints 30.
Again using reduce is the functional way to do it. The above
basically boils down to:
int[] arr1 = [1, 2, 30];
int maxElement = arr1[1];
foreach( element; arr1[2..$] ) //2..$ is short hand for second
till last ($) element
{
maxElement = max( maxElement, element );
}
writeln( maxElement );
Sorry been using too much R, so my indexes are off by 1:
int[] arr1 = [1, 2, 30];
int maxElement = arr1[0];
foreach( element; arr1[1..$] ) //1..$ is short hand for second
till last ($) element
{
maxElement = max( maxElement, element );
}
writeln( maxElement );
Thx guys. Now I try out the split function. I read the file as a
single string?
auto arr = split(cast(string)read(filename),",");
where the file has "A", "B", "C"
and I get the output ["\"A\"", " \"B\"", " \"C\"\n"]
I can understand that read functions reads the endl but what does
it with the quotation marks? how can I modify read so I get just
["A", "B", "C"]