When learning a new language its always difficult to google the meaning of operators. I ran into this problem when reading erlang source and trying to figure out what the hash sign does. Searching for "erlang hash" would always lead to articles about computing MD5 hashes in erlang. I had to resort to asking the question on stack overflow.So for the sake of the next person who googles this "What does the hash operator do in erlang?"

 

Defining Records

Record is a compound data type in erlang which gives named access to the elements it contains similar to a struct in c. To use records we must first define their structure:

    -record(record_name, {element_mame=optional_default_value}).

for example:

    -record(car, {model,year,color=blue}).

 

Initializing Records

To initialize we use the aforementioned hash sign as a prefix to the statement. Notice that I can choose not to supply values for the elements, elements with default values will have those assigned others will remain undefined.

    Car1 = #car{model=civic,year=2007,color=green},
    Car2 = #car{model=mazda,color=green}.

 

Accessing Records

To access records we use our trusty hash operator again and use the element name to retrieve data:

    Car1 = #car{model=civic,year=2007,color=green},
    Car2 = #car{model=mazda,color=green},
    Car1Model = Car1#car.model.

 

Updating Records

Updating records is much like initializing except that any elements that we do not specify values for will retain retain previous values.

    Car1 = #car{model=civic,year=2007,color=green},
    Car2 = #car{model=mazda,color=green},
    Car3 = Car2#car{year=2003}.

 

Further Reading