Chapter 8
Notice that this call is similar to av_fetch(), and similarly returns an SV** that may 
be NULL if the requested key does not exist. Just like av_exists(), hv_exists() pro 
vides a simple way to avoid dealing with SV**s:
SV *sv;
// check that $hv{foo} exists
if (hv_exists(hv, "foo", strlen("foo"))) {
   // safely trust hv_fetch to return non NULL
   sv = *(hv_fetch(hv, "foo", strlen("foo"), 0));
} else {
   croak("fetch failed: hv does not contain value for key foo");
}
Aside from reading a specific key, the other common way to read from a hash 
is to iterate through its keys and values. This is done using the hv_iter functions. 
For example, here's a function that prints out the keys and values in a hash:
void print_hash (HV *hv) {
   SV *sv;
   I32 i, count;
   char *key_string;
   STRLEN len;
   // initialize the iteration
   count = hv_iterinit(hv);
   // loop over key/value pairs
   for (i = 1; i <= count; i++) {
      sv = hv_iternextsv(hv, &key_string, (I32*) &len);
      printf("%s : %s\n", key_string, SvPV(sv, len));
    }
}
The preceding function uses two new Perl API calls, hv_iterinit() and 
hv_iternextsv(). The first initializes the iteration and returns the number 
of key value pairs in the hash:
count = hv_iterinit(hv);
188






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved