I am trying to match and remove elements from an array called @array. The elements to be removed must match the patterns stored inside an array called @del_pattern
my @del_pattern = ('input', 'output', 'wire', 'reg', '\b;\b', '\b,\b');
my @array = (['input', 'port_a', ','],
['output', '[31:0]', 'port_b,', 'port_c', ',']);
To remove the patterns contained in @del_pattern from @array, I loop through all the elements in @del_pattern and exclude them using grep.
## delete the patterns found in @del_pattern array
foreach $item (@del_pattern) {
foreach $i (@array) {
@$i = grep(!/$item/, @$i);
}
}
However, I have been unable to remove ',' from @array. If I use ',' instead of '\b,\b' in @del_pattern, element port_b, gets removed from the @array as well, which is not an intended outcome. I am only interested in removing elements that contain only ','.
input_ain@arrayshould that get removed? In other words, do patterns need to match or to be equal? For the comma you are saying that it should be equal -- "that contain only," (my emphasis).input_ain@array, it shouldn't get removed. The patterns need to match/^,$/, so that matching it means that it is equal.qr/.../(not just single quotes).