in reply to Inverse slices

Better how?

Shorter? Put the code in a sub.

More readable? Put the code in a sub.

Faster? Sort the indexes and use a merge sort-like approach written in C to filter out the unwanted elements.


Alternative approach if the array only has defined elements:

my @invslice = @array; @invslice[ @slice_idx ] = (); @invslice = grep defined, @invslice;

Update: Replaced undef @invslice[ @slice_idx ]; (which doesn't work) with @invslice[ @slice_idx ] = ();.

Replies are listed 'Best First'.
Re^2: Inverse slices
by ysth (Canon) on Nov 01, 2024 at 01:06 UTC
    While this use of undef works in all perl 5 versions I am aware of, it is in fact not documented. I prefer
    @invslice[ @slice_idx ] = ();
    --
    A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |
      While this use of undef works in all perl 5 versions I am aware of, it is in fact not documented
      Really? It didn't work on any perl version I tried. For me, ikegami's slice undef trick only undefined the last element in the slice.

        Fixed.