Hash

From FlightGear wiki
Jump to navigation Jump to search


std.Hash provides an object-oriented way to use hashes in Nasal.

new()

new([hash[, name]]);

Create a new Hash object.

hash
Optional, a hash as the initial value. Defaults to {}.
name
Optionally, a string as the internal hash name. Defaults to an empty string "".

Examples

var hash = std.Hash.new();
var hash = std.Hash.new({ aircraft: 'c172p'}, 'aircraft');

set()

set(key, value);

Create a new Hash object. Returns the hash object itself ("me"), allowing method chaining.

key
Key name of hash.
value
Value assigned to key.

Example

var hash = std.Hash.new();
hash.set('aircraft', 'c172p');

get()

get(key);

Get value by key. Returns the value assigned to the key.

key
The name of the hash key.

Example

var hash = std.Hash.new();
hash.set('aircraft', 'c172p');
logprint(LOG_ALERT, hash.get('aircraft')); # print "c172p"

clear()

clear();

Clears the entire hash to its default form {}. Returns the hash object itself ("me"), allowing method chaining.

Example

var hash = std.Hash.new();
hash.set('aircraft', 'c172p');
hash.clear();
logprint(LOG_ALERT, hash.get('aircraft')); # print nothing, get() will return nil

contains()

contains(key);

Checks whether the hash contains the given key. Returns true if the key exists in the hash, false otherwise.

key
The name of the hash key.

Example

var hash = std.Hash.new();
hash.set('aircraft', 'c172p');
hash.contains('aircraft'); # return true
hash.contains('terrain'); # return false

getName()

getName();

Returns the hash name given in the new() constructor. If no name was given in the new() method, it returns the empty string "".

Example

var hash = std.Hash.new(name: 'aircraft');
hash.getName(); # return "aircraft" string

getKeys()

getKeys();

Returns a vector with the key names contained in the hash. Useful for loop iteration.

Example

var hash = std.Hash.new();

hash.set('aircraft', 'c172p')
    .set('altitude', 5000)
    .set('airspeed', 100);

foreach (var key; hash.getKeys()) { # getKeys() return ["aircraft", "airspeed", "altitude"]
    logprint(LOG_ALERT, key, ' = ', hash.get(key)); # print each key with a value
}

addCallback()

addCallback(f);

Set a callback function that will be called each time the set() method is called.

f
Function as callback.

Example

var hash = std.Hash.new();
hash.addCallback(func(key, value) {
    logprint(LOG_ALERT, 'Set ', key, ' = ', value);
});
hash.set('aircraft', 'c172p'); # This will log print "Set aircraft = c172p"

keys2props()

keys2props(p);

Exports all keys from the internal hash to the property tree under the given root path as string or props.Node object. For each key in the hash, a corresponding property node is created (if it doesn’t already exist), but no values are assigned — only the nodes themselves are created. Returns the hash object itself ("me"), allowing method chaining.

p
Root property path (string) or a props.Node object where the keys will be exported. If a string is provided, it is automatically converted to a props.Node.

Example

var hash = std.Hash.new();
hash.set('aircraft', 'c172p')
    .set('altitude', 5000)
    .set('airspeed', 100);

hash.keys2props("/sim/my-data");

# Resulting property structure:
# /sim/my-data"/aircraft
# /sim/my-data"/airspeed
# /sim/my-data"/altitude
# (No values are set, only empty property nodes are created.)

hash2props()

hash2props(p);

Exports the entire internal hash to the property tree under the given root path as string or props.Node object. For each key–value pair in the hash, a corresponding property is created and its value is set to the hash value. Returns the hash object itself ("me"), allowing method chaining.

p
Root property path (string) or a props.Node object where the hash contents will be exported.

If a string is provided, it is automatically converted to a props.Node.

Example

var hash = std.Hash.new();
hash.set('aircraft', 'c172p')
    .set('altitude', 5000)
    .set('airspeed', 100);

hash.hash2props("/sim/my-data");

# Resulting property structure:
# /sim/my-data"/aircraft = "c172p"
# /sim/my-data"/airspeed = 100
# /sim/my-data"/altitude = 5000