in reply to Inverse slices

Here's a few ways (TIMTOWTDI).

#!/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 @newinvslice = @array; @newinvslice[@slice_idx] = (); @newinvslice = grep defined, @newinvslice; use Data::Dump 'dd'; dd { 'newinvslice' => \@newinvslice }; use List::Util qw( none ); my @new2invslice = @array[ grep { my $idx = $_; none { $idx == $_ } @slice_idx } 0 .. $#array ] +; use Data::Dump 'dd'; dd { 'new2invslice' => \@new2invslice }; my @new3invslice = @array; splice @new3invslice, $_, 1 for reverse @slice_idx; use Data::Dump 'dd'; dd { 'new3invslice' => \@new3invslice }; use List::Util qw( uniq ); my @new4invslice = @array[ (uniq @slice_idx, 0 .. $#array)[@slice_idx .. $#array ] ]; use Data::Dump 'dd'; dd { 'new4invslice' => \@new4invslice };

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", ], } { newinvslice => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], } { new2invslice => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], } { new3invslice => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], } { new4invslice => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], }

Replies are listed 'Best First'.
Re^2: Inverse slices
by tybalt89 (Monsignor) on Nov 01, 2024 at 22:03 UTC

    Found another one...

    use List::AllUtils qw( singleton ); my @new6invslice = @array[ singleton 0 .. $#array, @slice_idx ];

    If that's not better, we need a better definition of 'better'. (WARNING: possible infinite recursion encountered!)

Re^2: Inverse slices
by LanX (Saint) on Nov 01, 2024 at 15:29 UTC
    Some suggestions, while I wait for Ubuntu to upgrade...

    My comments in all CAPS.

    Most importantly - regarding @newinvslice - Ikegami was right to point out that an undef value is not a sufficiently safe criteria (AKA the semi-predicate problem)

    I have a solution using the ref of a scalar var which MUST be unique.

    A bit clumsy, because Perl has no === operator, but I am open for more elegant suggestions. :)

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11162546 use warnings; use Data::Dump 'dd'; use Test::More; $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]; my $expected = [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ]; dd { 'array' => \@array, 'slice_idx' => \@slice_idx, 'slice' => \@slice, 'expected' => $expected, }; { my $TEST = "not in idx hash"; my %slice_idx; @slice_idx{@slice_idx} = (); # NO MAP NEEDED my @invslice_idx = grep { ! exists $slice_idx{$_} } 0 .. $#array; # Inverse slice. my @invslice = @array[@invslice_idx]; is_deeply(\@invslice, $expected, $TEST ) or diag dd { 'idx' => \@invslice_idx, 'invslice' => \@invslice }; } { my $TEST = "not delete value"; my @newinvslice = @array; my $delete; @newinvslice[@slice_idx] = (\$delete) x @slice_idx; # UNIQUE REF V +ALUE @newinvslice = grep { ref $_ ne "SCALAR" or $_ ne \$delete } @ne +winvslice; is_deeply(\@newinvslice ,$expected, $TEST) or diag dd { 'newinvslice' => \@newinvslice }; } { my $TEST = "splice backwards"; my @new3invslice = @array; splice @new3invslice, $_, 1 for sort { $a < $b } @slice_idx; # ENS +URE IDX SORTED is_deeply(\@new3invslice, $expected, $TEST) or diag dd { 'new3invslice' => \@new3invslice }; } done_testing;

    { array => ["aa" .. "zz"], expected => [ "aa" .. "af", "ah" .. "am", "ao" .. "bp", "br" .. "cn", "cp", "cq", "cs" .. "zz", ], slice => ["ag", "an", "bq", "co", "cr"], slice_idx => [6, 13, 42, 66, 69], } ok 1 - not in idx hash ok 2 - not delete value ok 3 - splice backwards 1..3

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    Updates

    Bugfix s/and/or/

      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", ], }