Hello,

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?

-- fxn


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.

I think simpler would be (not tested):
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)));
      }
 }

But on the other way a memcpy() coud be do the same, I think.

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.

As I understand a slice it is a "part" of the array, isn't it?

regards
Reinhard

Reply via email to