From 475a1810282f338f2808fb1fdc66b4b5273aabb4 Mon Sep 17 00:00:00 2001 From: JohnathanFL Date: Mon, 15 Jul 2019 17:20:56 -0500 Subject: [PATCH 1/2] Add multidimensional array example --- doc/langref.html.in | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index bb54a91c8..ca473cb38 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -1731,6 +1731,27 @@ test "array initialization with function calls" { assert(more_points[4].y == 6); assert(more_points.len == 10); } + +// Multidimensional arrays are declared by simply adding another array before the existing array +var mat4x4 = [4][4]f32{ + [_]f32{1.0, 0.0, 0.0, 0.0}, + [_]f32{0.0, 1.0, 0.0, 1.0}, + [_]f32{0.0, 0.0, 1.0, 0.0}, + [_]f32{0.0, 0.0, 0.0, 1.0} +}; +test "multidimensional arrays" { + // Multidimensional arrays can be accessed as expected from other languages... + assert(mat4x4[1][1] == 1.0); + + // or iterated over like any other array + for (mat4x4) |row, rowNum| { + for (row) |column, colNum| { + if (rowNum == colNum) { + assert(column == 1.0); + } + } + } +} {#code_end#} {#see_also|for|Slices#} {#header_close#} From 23dd7f452771b5f6af9d34ad2fb29a82d66b18f3 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 16 Jul 2019 13:13:21 -0400 Subject: [PATCH 2/2] organize the docs and some rewording --- doc/langref.html.in | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index ca473cb38..b5fe464c3 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -1731,29 +1731,38 @@ test "array initialization with function calls" { assert(more_points[4].y == 6); assert(more_points.len == 10); } + {#code_end#} + {#see_also|for|Slices#} -// Multidimensional arrays are declared by simply adding another array before the existing array -var mat4x4 = [4][4]f32{ - [_]f32{1.0, 0.0, 0.0, 0.0}, - [_]f32{0.0, 1.0, 0.0, 1.0}, - [_]f32{0.0, 0.0, 1.0, 0.0}, - [_]f32{0.0, 0.0, 0.0, 1.0} + {#header_open|Multidimensional Arrays#} +

+ Mutlidimensional arrays can be created by nesting arrays: +

+ {#code_begin|test|multidimensional#} +const std = @import("std"); +const assert = std.debug.assert; + +const mat4x4 = [4][4]f32{ + [_]f32{ 1.0, 0.0, 0.0, 0.0 }, + [_]f32{ 0.0, 1.0, 0.0, 1.0 }, + [_]f32{ 0.0, 0.0, 1.0, 0.0 }, + [_]f32{ 0.0, 0.0, 0.0, 1.0 }, }; test "multidimensional arrays" { - // Multidimensional arrays can be accessed as expected from other languages... + // Access the 2D array by indexing the outer array, and then the inner array. assert(mat4x4[1][1] == 1.0); - // or iterated over like any other array - for (mat4x4) |row, rowNum| { - for (row) |column, colNum| { - if (rowNum == colNum) { - assert(column == 1.0); + // Here we iterate with for loops. + for (mat4x4) |row, row_index| { + for (row) |cell, column_index| { + if (row_index == column_index) { + assert(cell == 1.0); } } } } {#code_end#} - {#see_also|for|Slices#} + {#header_close#} {#header_close#} {#header_open|Vectors#}