Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two(multi) dimensional array in Erlang

Tags:

arrays

erlang

These days I'm solving Project Euler problems in Erlang.

Since I'm a C++ programmer from the beginning, sometimes I really want to code using two dimensional arrays.

One of my idea is to use tuples and lists like this:

List=[{X,0}||X<-lists:seq(1,3)]
{1,0}
{2,0}
{3,0}

Is there nice way to implement multidimensional arrays in Erlang?

like image 389
Sungwon Jeong Avatar asked Feb 11 '09 13:02

Sungwon Jeong


People also ask

Can we use multidimensional arrays for adding two matrices?

In this program, the user is asked to enter the number of rows r and columns c . Then, the user is asked to enter the elements of the two matrices (of order rxc ). We then added corresponding elements of two matrices and saved it in another matrix (two-dimensional array). Finally, the result is printed on the screen.

Can arrays have multiple dimensions?

More than Three Dimensions Although an array can have as many as 32 dimensions, it is rare to have more than three.

What is multi-dimensional array with example?

A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.

Can C language handle multidimensional arrays?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays.


2 Answers

See array module but for multidimensional access you have to write your own wrapper. If any of your dimension is short and access is mostly read you can use tuples and use erlang:element and erlang:setelement. Own wrapper is recommended anyway.

like image 98
Hynek -Pichi- Vychodil Avatar answered Sep 28 '22 12:09

Hynek -Pichi- Vychodil


Try array(actually dict) with {X, Y, Z} as a key. It's look like 3d array ;)

like image 25
JLarky Avatar answered Sep 28 '22 12:09

JLarky