Method code for $db.match_begin()

[Turn on line numbering]
arg key;
var matches, entry;

// First check if we're using $trie frob
if (type(database) == 'frob) {
    matches = (| (database.match_begin(key))[2] |);
    if (matches == ~keynf)
        throw(~matchnf, "No entries in the database match that key.");
    if (matches == ~ambig)
        throw(~ambig, "More than one object matches that key.");
    return matches;
}

// use match_begin of the key, return the value
matches = [(| .exact_match(key) |)];
if (!matches[1]) {
    matches = [];
    for entry in (database) {
        if (match_begin(entry[1], key))
            matches = setadd(matches, entry[2]);
    }
}
if (matches) {
    if (matches.length() == 1)
        return matches[1];
    else
        throw(~ambig, "More than one object matches that key.", matches);
} else {
    throw(~matchnf, "No entries in the database match that key.");
}

// Created 27-Mar-1995 as a part of ColdCore, see: @help Credit

Tlon