MapCreate
- creates a map/dictionary object (holding key-value pairs)

Map functions
(AmiBroker 6.90)


SYNTAX MapCreate( hash_size = 0 )
RETURNS MAP
FUNCTION Creates a new Map object and returns the map. In AFL, a Map is a data structure that stores key-value pairs, similar to dictionaries in other programming languages. Map keys are strings. Values can be of any type.

Advanced users may pass hash table size if they want to store lots of data in a map in hash_size parameter. Hash size should be prime number larger than maximum expected number of elements in the map

EXAMPLE // Map creation
// larger hash table size improves performance should be prime number larger than number of elements expected
m = MapCreate( 997 );

// assigning values
m["MSFT"] = 1;
m["NVDA"] = 2;
m["CSCO"] = 3;

// accessing value by key
value = m[ Name() ]; // using function return value as a key
value2 = m[ "MSFT" ]; // literal key

// checking for key existence
value = m[ Name() ];
if (IsNull(value))
{
  printf("Key not found in Map");
}
else
{
  printf("%g", value);
}

// EXAMPLE 2

// Passing map arguments to user functions
function Test(x)
{
  x["key"] = 1;
}

m2 = Map();

// IMPORTANT CAVEAT:
// UNLIKE other data types,
// Maps are always passed by reference so this call modifies its contents
Test(m2);

// Check the modified map
value = m2["key"];

if (IsNull(value))
{
  printf("Key not found in Map");
}
else
{
  printf("%g", value);
}
SEE ALSO

References:

The MapCreate function is used in the following formulas in AFL on-line library:

More information:

See updated/extended version on-line.