On Nov 3, 2005, at 9:05, Reinhard Pagitsch wrote:
Xavier Noria wrote:
I founded nothing about slices in perlguts, perlxs*, or perlapi
(where are they documented?), so I wrote this little utility to
take a slice from AV* data using the indices in AV* indices
(integers), and put the result in AV* out (indices and out are
guaranteed to have the same length):
void __slice(AV* indices, AV* data, AV* out) {
int i;
I32 last_index;
I32 index;
SV* val;
last_index = av_len(indices);
for (i = 0; i <= last_index; ++i) {
index = SvIVX(*av_fetch(indices, i, 0));
val = *av_fetch(data, index, 0);
av_store(out, i, newSVsv(val));
}
}
I am just starting to play around with the C API. Is this code
right as far as XS is concerned? Is there a better idiom?
I do not understand what you mean. The code you posted here is only
to make a copy of the array data and put it into the array out.
Yes, the subroutine has a bad name, it should be called
__slice_and_copy.
void __slice(AV* data, AV* out) {
int i;
I32 max;
max = av_len(data);
for (i = 0; i <= max; ++i) {
av_store(out, i, newSVsv(*av_fetch(data, i, 0)));
}
}
The difference is that you're copying the entire data into out,
whereas in the code I sent only the slice indicated by indices is
copied. My code is (wants to be) the C equivalent of
@out = @[EMAIL PROTECTED];
In the book "Extending and Embedding Perl" from TIM JENNESS and
SIMON COZENS I found something about slices, but I do not know if
it is what you want.
I have yet to buy it, would you please summarise what they say about
array slices?
-- fxn