How to resolve the algorithm Associative array/Creation step by step in the Chapel programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Associative array/Creation step by step in the Chapel programming language

Table of Contents

Problem Statement

The goal is to create an associative array (also known as a dictionary, map, or hash).

Related tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Associative array/Creation step by step in the Chapel programming language

Source code in the chapel programming language

// arr is an array of string to int. any type can be used in both places.
var keys: domain(string);
var arr: [keys] int;

// keys can be added to a domain using +, new values will be initialized to the default value (0 for int)
keys += "foo";
keys += "bar";
keys += "baz";

// array access via [] or ()
arr["foo"] = 1;
arr["bar"] = 4;
arr("baz") = 6;

// write auto-formats domains and arrays
writeln("Keys: ", keys);
writeln("Values: ", arr);

// keys can be deleted using -
keys -= "bar";

writeln("Keys: ", keys);
writeln("Values: ", arr);

// chapel also supports array literals
var arr2 = [ "John" => 3, "Pete" => 14 ];

writeln("arr2 keys: ", arr2.domain);
writeln("arr2 values: ", arr2);


  

You may also check:How to resolve the algorithm Count in octal step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the ERRE programming language
You may also check:How to resolve the algorithm 21 game step by step in the Factor programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the PL/M programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Phixmonti programming language