73: #initialize a value to count where we are in the array in orde +r to 74: #delete the lines we do not need 75: my $i = 0; 76: my $item; 77: my $item2; 78: foreach $item (@data_file){ 79: foreach $item2 (@lines){ 80: if ($item =~ /$item2/){ 81: splice(@data_file,$i,1); 82: } 83: } 84: $i++ 85: }
From the perlsyn man page: <QUOTE>If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don’t do that. </QUOTE> Also you should quotemeta any outside data used in a regular expression.
You can accomplish what you want like this:
#initialize a value to count where we are in the array in order to #delete the lines we do not need foreach my $index ( reverse 0 .. $#data_file ) { foreach my $item ( @lines ) { if ( $data_file[ $index ] =~ /\Q$item/ ) { splice @data_file, $index, 1; } } }
In reply to Re: Why does the array not reset each time I call the function?
by jwkrahn
in thread Why does the array not reset each time I call the function?
by tuxy94
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |