qumsieh has asked for the wisdom of the Perl Monks concerning the following question:

Is there an equivalent of splice() that's accessible via XS?
The closest I found was av_delete() which effectively undefs the element at the given index. Any way to shrink the AV?
Thanks.

Replies are listed 'Best First'.
Re: No av_splice function?
by Util (Priest) on May 02, 2008 at 21:11 UTC

    My XS is rusty, but given that "splice" is not mentioned in perlapi or Extending and Embedding Perl, and that Perl's implementation of the splice opcode is about 130 SLOC (vs 7 SLOC for the push opcode and 36 SLOC for the delete opcode, due to their use of av_push and av_delete), I would say that no equivalent of splice() is accessible via XS.

    Perhaps you can adapt the code that Perl itself uses; it is in file pp.c, in function pp_splice.
    The large "if (diff < 0)" block handles shrinking the AV.

Re: No av_splice function?
by syphilis (Archbishop) on May 03, 2008 at 00:20 UTC
    Any way to shrink the AV?

    There's av_pop, av_shift and av_fill (which can be used instead of repeatedly calling av_pop).

    But, of course, they're a bit limiting if you want to shrink the AV by removing middle elements :-)

    Cheers,
    Rob
      Thanks for both replies.
      My inclination is to define some sort of threshold, such that when that many elements of my AV (say 50%) are undefined, then I replace it with a new AV. I'm guessing that this will be faster and more efficient than continually resizing my AV anytime an element needs to be deleted.
      Thanks again.