Finding Max Value of Column in Multi-Dimesional Array

Jordan Wilson wilsonjord at gmail.com
Fri Jul 5 03:02:29 UTC 2019


On Friday, 5 July 2019 at 00:54:15 UTC, Samir wrote:
> Is there a cleaner way of finding the maximum value of say the 
> third column in a multi-dimensional array than this?
> int[][] p = [[1,2,3,4], [9,0,5,4], [0,6,2,1]];
> writeln([p[0][2], p[1][2], p[2][2]].max);
>
> I've tried the following
> writeln([0, 1, 2].map!(p[a][2]).max);
>
> but get an "Error: undefined identifier a" error.
>
> I know there doesn't seem to be much of a difference between 
> two examples but my real-world array is more complex which is 
> why I'm looking for a more scalable option.
>
> Thanks
> Samir

p.map!(a => a[2]).maxElement.writeln; // 5
p.map!"a[2]".maxElement.writeln; // 5

Or, modifying your example:
writeln([0,1,2].map!(a => p[a][2]).maxElement;

Thanks,

Jordan


More information about the Digitalmars-d-learn mailing list