Author Topic: sklick-c: how do I check if a key exists in a hash table ?  (Read 628 times)

jcelle

  • Senior Community Member
  • Posts: 116
  • Hero Points: 4
sklick-c: how do I check if a key exists in a hash table ?
« 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.

jcelle

  • Senior Community Member
  • Posts: 116
  • Hero Points: 4
Re: sklick-c: how do I check if a key exists in a hash table ?
« Reply #1 on: June 28, 2020, 10:19:16 am »
oups  :o
Just realized there was a slick-c dedicated forum, going there...

Graeme

  • Senior Community Member
  • Posts: 2471
  • Hero Points: 325
Re: sklick-c: how do I check if a key exists in a hash table ?
« Reply #2 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);

jcelle

  • Senior Community Member
  • Posts: 116
  • Hero Points: 4
Re: sklick-c: how do I check if a key exists in a hash table ?
« Reply #3 on: June 29, 2020, 06:51:04 am »
Nice one, works like a charm !
Thanks Graeme.