SlickEdit Community

General => General Programming => Topic started by: jcelle on June 28, 2020, 07:11:43 AM

Title: sklick-c: how do I check if a key exists in a hash table ?
Post by: jcelle on June 28, 2020, 07:11:43 AM
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: [Select]
if( null == myHash[1965] )...Only thing I found is iterate over _get_hashtab_keys() and look for my key which looks weird.

Thanks for any advice.
Title: Re: sklick-c: how do I check if a key exists in a hash table ?
Post by: jcelle on June 28, 2020, 10:19:16 AM
oups  :o
Just realized there was a slick-c dedicated forum, going there...
Title: Re: sklick-c: how do I check if a key exists in a hash table ?
Post by: Graeme on June 29, 2020, 06:32:50 AM
Use _indexin.  It seems to be missing from the description of hash tables in the help.

Code: [Select]
/**
 * @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);
Title: Re: sklick-c: how do I check if a key exists in a hash table ?
Post by: jcelle on June 29, 2020, 06:51:04 AM
Nice one, works like a charm !
Thanks Graeme.