Chapter 8
Creation
The simplest way to create an AV is to use the newAV() function:
AV *av = newAV();
If you have an array of SV*s, then you can create an array from them using
av_make():
AV *av;
SV *sv_array[3];
sv_array[0] = newSVpv("foo",0);
sv_array[1] = newSVpv("bar",0);
sv_array[2] = newSVpv("baz",0);
av = av_make(3, sv_array); // create an array from the three SVs
Fetching Values
AVs support access by index as well as the familiar pop and shift operations. You
can fetch an SV from an array using the av_fetch() function:
SV **svp;
svp = av_fetch(av, 10, 0); // fetch $av[10] (the 0 indicates this isn t an
// lvalue)
if (!svp) croak("fetch failed: av doesn t have a tenth element!");
Notice that the return value from av_fetch() is a pointer to a pointer to SV (that is,
an SV**) not a normal pointer to SV (that is, SV*). If you try to fetch a value that
doesn't exist, then av_fetch() will return a NULL pointer. Be sure to check the return
value before dereferencing or you'll end up with a segmentation fault if the ele
ment doesn't exist. The preceding code checks the return value and calls croak()
the Perl API version of die if av_fetch() returns NULL.
However, you can skip testing the return value from av_fetch() if you know the
element exists. You can get this information using av_exists(), which tests whether
an index exists in an AV:
SV *sv;
if (av_exists(av, 9)) { // check that the 10th element exists
sv = *(av_fetch(av, 9, 0)); // safely trust av_fetch to return non NULL
} else {
croak("av doesn t have a tenth element!");
}
184
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