How to handle hash value type in Perl XS -


i need process hash value depends on value type. here code problem:

i32 keys = hv_iterinit(hash); (i32 = 0; < keys; i++) {   char *key = null;   i32 key_length = 0;   sv *value = hv_iternextsv(hash, &key, &key_length);   // svrok(value);   if (svtype(svrv(value)) < svt_pvav)   {     // handle scalar     printf("key %s has scalar value\n", key);   }   else if (svtype(svrv(value)) == svt_pvav)   {     // handle array     printf("key %s has array value\n", key);   }   else if (svtype(svrv(value)) == svt_pvhv)   {     // handle hash     printf("key %s has hash value\n", key);   } } 

if don't use commented line, have problem scalar values. example following hash {a => "b", c => {d => "e"}} produce output:

key c has hash value key d has scalar value 

so here questions:

  1. do have reference returned hv_iternextsv() or returns scalar?
  2. why don't see scalar value output key a.

update.

my mistake in working result of hv_iternextsv(). thinking reference. here how working code looks like:

i32 keys = hv_iterinit(hash); (i32 = 0; < keys; i++) {   char *key = null;   i32 key_length = 0;   sv *value = hv_iternextsv(hash, &key, &key_length);   if (!svrok(value))   {     // handle scalar   }   else   {     if (svtype(svrv(value)) == svt_pvav)     {       // handle array     }     else if (svtype(svrv(value)) == svt_pvhv)     {       // handle hash     }   } } 

do have reference returned hv_iternextsv() or returns scalar?

it returns scalar. hashes values can scalars. scalars can references ($h{x} = [];), need not ($h{y} = 123;).

why don't see scalar value output key a.

there's no way possibly return said does, seeing hash has no key named d. hash provided, code outputs following:

key has scalar value key c has hash value 

but it's more of coincidence else got right answer. svtype(svrv(value)) when value isn't reference??? makes no sense! fixed code follows:

use strict; use warnings;  use inline c => <<'__eoi__';    void print_keys(hv* hash) {     char *key;     i32 key_length;     sv *value;      hv_iterinit(hash);     while (value = hv_iternextsv(hash, &key, &key_length)) {       if (svrok(value)) {         sv * const referenced = svrv(value);         if (svtype(referenced) == svt_pvav) {           printf("the value @ key %s reference array\n", key);         }         else if (svtype(referenced) == svt_pvhv) {            printf("the value @ key %s reference hash\n", key);         }         else {            printf("the value @ key %s reference\n", key);         }       } else {         printf("the value @ key %s not reference\n", key);       }     }   }  __eoi__  print_keys({a => "b", c => {d => "e"}}); 

output:

the value @ key not reference value @ key c reference hash 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -