in reply to Re: Ignore a range of numbers in a List
in thread Ignore a range of numbers ina List

Thanks shmem. I have realized the problem with my code initially and couldn't think of a way to fix the offset going outside of the @copy array

The second solution using array values is precise and clear . I have tried the code and it yields right results for all the test iterations.

  • Comment on Re^2: Ignore a range of numbers in a List

Replies are listed 'Best First'.
Re^3: Ignore a range of numbers in a List
by poj (Abbot) on Jun 25, 2017 at 19:32 UTC

    Alternatively, you can just use splice with a negative offset to remove elements from the end

    #!perl use strict; use warnings; while (<DATA>){ chomp; my $aref = [ split /\s+/,$_ ]; print join ' ', 'Original Array:', @$aref,"\n"; my $start; my @copy=(); my $i=0; for (@$aref){ push @copy,$_; if ($_ == 6){ $start = $i unless defined $start; } elsif (defined $start && $_ == 7){ splice @copy, $start-$i-1; $start = undef; } ++$i; } print join ' ','Results:',@copy,"\n"; } __DATA__ 1 6 2 2 7 1 6 99 99 7 1 2 2 1 1 6 7 2 1 6 2 2 7 1 6 99 99 7 2 7 6 2 6 7 2 7 1 6 7 7 2 7 6 2 6 2 7 6 7 1 6 7 7 6 8 1 6 7
    poj
Re^3: Ignore a range of numbers in a List
by shmem (Chancellor) on Jun 25, 2017 at 18:14 UTC

    There's another way to do it: use decreasing indices and iterate from the end to the beginning. This keeps the arrays aligned after splice.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'