Perl:从数组中删除带有特殊字符的记录
我想从数组中查找并删除记录。我正在使用选项1,但它不起作用。数组中的值1的值类似于Perl:从数组中删除带有特殊字符的记录,perl,Perl,我想从数组中查找并删除记录。我正在使用选项1,但它不起作用。数组中的值1的值类似于 abc::record1 def:record2 ghf::record3 poi::record4 需要比较的值类似于变量项中的record1、record2、record3 备选案文1: my @found = grep( /^$item$/, @array1 ); if (@found){ @array = eval { grep(!/$item/, @array1); };
abc::record1
def:record2
ghf::record3
poi::record4
需要比较的值类似于变量项中的record1、record2、record3
备选案文1:
my @found = grep( /^$item$/, @array1 );
if (@found){
@array = eval { grep(!/$item/, @array1); };
@array1 = @array;
}
我是否应该尝试使用选项2,这是否会在字符串/行中查找精确匹配或任何值
选择2
@array = grep { $_ ne $item } @array1;
@array1 = @array;
感谢只需取消拼接即可删除数组元素,语法如下 拼接@数组,$first_索引[,$number_替换的_元素][,@values]
- @数组是要修改的数组
- $number_of_replaced_element:要修改或删除的元素数
- @值:要插入的值
只要我理解你的问题,我建议选择3:
foreach (@array1){
if ($item == $_) { < - This is the current element of the array
Do What you want
}
}
foreach(@array1){
如果($item=$\{<-这是数组的当前元素
做你想做的
}
}
不明白您的要求。美元项目是什么?你能显示想要的输出吗?这看起来像是猎枪式的“思想流派”
foreach (@array1){
if ($item == $_) { < - This is the current element of the array
Do What you want
}
}