in reply to Removing an element from an AV?

Here is one way to do it simply using av_shift and av_push iterating over the AV* It is probably not the best or fastest way. In general it is not recommended to operate on an array while iterating over it, although it can be done in certain circumstances. An alternative method, that removes the shift overhead is simply to create a new temp AV*, iterate over your original AV* extracting the values and doing the comparison and push into your new AV*. Don't forget to destroy your excess AV* or you will leak. See perlguts for all the handy macros you have available.....

use Inline 'C'; my @ary = ('aa'..'az', 'aj'); my $rc = remove_str( \@ary, 'aj' ); print"$rc\n@ary\n"; __END__ __C__ int remove_str( SV* av_ref, SV* remove ) { AV* terms; SV* element; I32 num, i; STRLEN len; int retval = 0; terms = (AV *)SvRV(av_ref); num = av_len(terms); for( i=0; i<=num; i++ ) { element = av_shift(terms); if( strcmp( SvPV(element,len), SvPV(remove,len) ) ) av_push(terms, element); else retval++; } return retval; }

cheers

tachyon

Replies are listed 'Best First'.
Re^2: Removing an element from an AV?
by Anonymous Monk on Oct 08, 2004 at 18:39 UTC
    Thanks! Works perfectly.