
list_to_hash(+KeyPos, +ValPos, +List, ?Table)

   Enter a list of items into a (new or existing) hash table

Arguments
   KeyPos              Key position (non-negative integer)
   ValPos              Value position (non-negative integer)
   List                List of structures
   Table               A hash table or variable

Type
   library(hash)

Description

        This predicate takes a list of structures, and makes an entry
        into a hash table for every list element.  The KeyPos argument 
        determines which structure argument will be used as the key,
        and ValPos determines which structure argument will be used as
        the value.  KeyPos and/or ValPos can be set to 0, indicating that
        the whole structure should be used, rather than just one argument.
        This makes this predicate quite versatile and avoids the need
        to construct auxiliary lists.
        
        The hash table itself may already exist, in which case the new
        entries will be added to the table, possibly replacing existing
        entries with the same key.  If Table is a free variable, a fresh
        hash table will be implicitly created (as with hash_create/1).
        
        When working with struct-notation, KeyPos and ValPos can also
        be symbolic terms like `author of book'.
        
        

Modes and Determinism
   list_to_hash(+, +, +, -) is det
   list_to_hash(+, +, +, +) is det

Examples
   
        % To enter a Key-Value list, use positions 1 and 2:
        ?- Data = [a-one,c-three,b-two],
           list_to_hash(1, 2, Data, Hash),
           hash_get(Hash, b, X).

        X = two
        Yes (0.00s cpu)

        % To enter structures using one of their arguments as the key:
        ?- Data = [emp(jo,12),emp(ed,7),emp(al,4)],
           list_to_hash(2, 0, Data, Hash),
           hash_get(Hash, 7, X).

        X = emp(ed, 7)
        Yes (0.00s cpu)

        % You can incrementally add to/update an existing hash table:
        ?- hash_create(Hash),
           hash_set(Hash, d, four),
           list_to_hash(1, 2, [a-one,c-three,b-two], Hash),
           list_to_hash(1, 2, [c-new,e-five], Hash),
           hash_list(Hash, Keys, Values).

        Keys = [d, e, a, b, c]
        Values = [four, five, one, two, new]
        Yes (0.00s cpu)
    

See Also
   hash_create / 1, hash_set / 3, struct / 1
