How to fix the semi-predicate problem using undef :)
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11162546 use warnings; $SIG{__WARN__} = sub { die @_ }; # Is there a better way to get an inverse slice than to do something l +ike: my @array = ('aa'..'zz'); my @slice_idx = (6,13,42,66,69); # Slice my @slice = @array[@slice_idx]; use Data::Dump 'dd'; dd { 'array' => \@array, 'slice_idx' => \@slice_idx, 'slice' => \@slice }; my %slice_idx = map { $_ => 1 } @slice_idx; my @invslice_idx = grep { ! exists $slice_idx{$_} } 0 .. $#array; # Inverse slice. my @invslice = @array[@invslice_idx]; use Data::Dump 'dd'; dd { 'idx' => \@invslice_idx, 'invslice' => \@inv +slice }; my @wanted = 0 .. $#array; @wanted[@slice_idx] = (); my @new6invslice = @array[ grep defined, @wanted ]; use Data::Dump 'dd'; dd { 'new6invslice' => \@new6invslice };
Outputs:
{ array => ["aa" .. "zz"], slice => ["ag", "an", "bq", "co", "cr"], slice_idx => [6, 13, 42, 66, 69], } { idx => [0 .. 5, 7 .. 12, 14 .. 41, 43 .. 65, 67, 68, 70 .. 675], invslice => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], } { new6invslice => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], }
In reply to Re^3: Inverse slices
by tybalt89
in thread Inverse slices
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |