Newbie question on arrays and indexing

Wiger Ulf <>
Thu Mar 20 22:19:58 CET 2003

From: "Webb Sprague" <>

> Shawn Pearce wrote:
> >Try element/2:
> >>T = {1, 2, 3, 4}.
> >{1, 2, 3, 4}
> >>element(1, T).
> >1
> >>element(3, T).
> >3

> I was just wondering if there were a more graceful way to do it.  I will
> look into more variations on element.

Tuples are pretty good for this. There is a function called
erlang:make_tuple(Size,DefaultValue) that allows you to cheaply create
even a rather large tuple.

For _very_large arrays, tuples are not so great, and neither are lists.
You can use ets, or perhaps a utility called dynarray, that you will find
under user contributions at Dynarray basically behaves as an
infinite array. It's implemented using a dynamically expanding tuple tree.
Its main drawback is in debugging, as the printouts can get unwieldy. In
respect, ets tables are very nice.


More information about the erlang-questions mailing list