Brann Douglas B Capt AFRL/DEBE <[EMAIL PROTECTED]> writes:
>Hi, I have what seemed like an easy problem a few hours ago, but I've been
>tring to figure out if an element of a hash table is defined or not.
>
>Essentially, I have a hash of arrays, and if the hash element is defined, I
>want to insert the element into it, but if the element isn't defined, I need
>to create it and add to it.
>
>Can somebody post a code snippet?

C<exists> and C<defined> are roughly as follows:

   SV **svp = hv_fetch(...);
   if (svp) {
      /* exists */
      if (SvOK(*svp)) {
         /* defined */ 
      }  
      if (SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) {
         /* ref to array */
      }
   }

But you can do part of what you want by passing a true value to final "lval"
parameter of hv_fetch() and similar:

Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)

If lval is true, then hv.c will create an SV for you and 'svp' in code
fragment above will never be NULL.


Reply via email to