Hello Monks,
Within a list , I want to ignore a section of numbers that start with 6 and extending to the 7 My code below does that only for the first set of numbers . I am trying to splice the array on the copy and not original array But looks like the loop ends before it can read the next 6 in the list . Not looking for any grep or regex solution for this task .
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my $i = 1; my $sixindex = 0; my @copy = @$aref; if (scalar(@$aref) >= 2) { foreach my $x (0..$#{$aref}) { if (defined($aref->[$x])) { if ($aref->[$x] == 6) { $flag = 1; $sixindex = $x; next; } if ($flag) { unless ($aref->[$x] == 7) { $i++; next; } elsif ($aref->[$x] == 7 ) { splice(@copy, $sixindex, $i + 1); $flag = 0; $i = 1; $sixindex = 0; next; } } } } } print Dumper \@copy;
./ignore_6_to_7.pl $VAR1 = [ 1, 1, 6, 99, 99, 7 ];
Result Expected [1, 1] by deleting all the numbers in the list starting with 6 and unt +il it finds the first available 7 .
In reply to Ignore a range of numbers ina List by pr33
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |