Programming Perl in C
You can get the same effect using av_len() to check the length of the array:
SV *sv;
if (av_len(av) >= 9) { // check that $#av >= 9
sv = *(av_fetch(av, 9, 0)); // safely trust av_fetch to return non NULL
} else {
croak("av doesn t have a tenth element!");
}
The av_len() function works the same way as the $#array magic value it returns
the last valid index in an array.
Combining the preceding functions, you can now write a function to iterate
through an array and print out each value:
void print_array (AV *av) {
SV *sv; // SV pointer to hold return from array
char *string; // string pointer to hold SV string value
STRLEN len; // unused length value for SvPV()
I32 i = 0; // loop counter
// loop over all valid indexes
for (i = 0; i <= av_len(av); i++) {
sv = *(av_fetch(av, i, 0)); // get the SV for this index
string = SvPV(sv, len); // get a stringified form of the SV
printf("%s\n", string); // print it out, one value per line
}
}
As I mentioned earlier, AVs also support a version of Perl's pop and shift built
ins. These functions, av_pop() and av_shift(), return regular SV* pointers rather
than the SV** pointers returned by av_fetch(). Using av_shift(), you could write a
destructive version of the for loop just shown:
18
185
5
footer
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