General > General Programming

sklick-c: how do I check if a key exists in a hash table ?


Hello community,
I came across this one today...
How can I check if a key has already been defined in a hash table ?
Looks pretty simple but I am stuck.
Should I test for null ?

--- Code: ---if( null == myHash[1965] )...
--- End code ---
Only thing I found is iterate over _get_hashtab_keys() and look for my key which looks weird.

Thanks for any advice.

oups  :o
Just realized there was a slick-c dedicated forum, going there...

Use _indexin.  It seems to be missing from the description of hash tables in the help.

--- Code: ---/**
 * @return
 * Returns non-zero pointer to element if element exists at index given.
 * @param index      index or key corresponding to item to look for
 * @example
 * <pre>
 * defmain()
 * {
 *     typeless a:[];
 *     a:['sdf']._makeempty();  // a._indexin('sdf') will be false
 *     a:['key1']=5;         // a._indexin('key1') will be true
 * }
 * </pre>
 * @see _el
 * @see _nextel
 * @see _makeempty
 * @see _isempty
 * @see _varformat
 * @see _indexin
 * @see _sort
 * @categories Miscellaneous_Functions
typeless *_indexin(_str index),
         *_sc_lang_collection::_indexin(_str index);

--- End code ---

Nice one, works like a charm !
Thanks Graeme.


[0] Message Index

Go to full version