我通过将行放入数组来打开文件。在此文件内部基于包含重复值的正则表达式。如果正则表达式匹配,我想计算它。正则表达式可能如下所示
$b =~ /\/([^\/]+)@@/
。我想匹配$1
值。
my @array = do
{
open my $FH, '<', 'abc.txt' or die 'unable to open the file\n';
<$FH>;
};
以下是我的方式,它将在我的文件中获得相同的行。谢谢你的帮助。
foreach my $b (@array)
{
$conflictTemp = 0;
$b =~ /\/([^\/]+)@@/;
$b = $1;
#print "$b\n";
foreach my $c (@array)
{
$c =~ /\/([^\/]+)@@/;
$c = $1;
if($b eq $c)
{
$conflictTemp ++;
#print "$b , $c \n"
#if($conflictTemp > 1)
#{
# $conflict ++;
#}
}
}
}
以下是一些样本数据,两个句子是重复的
/a/b/c/d/code/Debug/atlantis_digital/c/d/code/Debug/atlantis_digital.map@@/main/place.09/2
/a/b/c/d/code/C5537_mem_map.cmd@@/main/place.09/0
/a/b/c/d/code/.settings/org.eclipse.cdt.managedbuilder.core.prefs@@/main/4
/a/b/c/d/code/.project_initial@@/main/2
/a/b/c/d/code/.project@@/main/CSS5/5
/a/b/c/d/code/.cproject@@/main/CSS5/10
/a/b/c/d/code/.cdtproject@@/main/place.09/0
/a/b/c/d/code/.cdtproject@@/main/place.09/0
/a/b/c/d/code/.cdtbuild_initial@@/main/2
/a/b/c/d/code/.**cdtbuild@@**/main/CSS5/2
/a/b/c/d/code/.**cdtbuild@@**/main/CSS5/2
/a/b/c/d/code/.ccsproject@@/main/CSS5/3
答案 0 :(得分:0)
看起来你正在尝试迭代数组的每个元素,通过模式匹配选择一些数据,然后计算dupes。那是对的吗?
不容易:
context.startActivity(Intent.createChooser(shareIntent, "Choose an app"));
然后,对于具有多个变量的变量(例如,存在重复):
my %count_of;
while ( <$FH> ) {
my ( $val ) = /\/([^\/]+)@@/;
$count_of{$val}++;
}
或者,如果你只是想玩'发现欺骗':
print join "\n", grep { $count_of{$_} > 1 } keys %count_of;
答案 1 :(得分:0)
问题已经通过前一个答案解决了 - 我只是想提供一种另类的味道;
%seen
哈希记录模式首次出现的行;启用use v5.12;
use warnings;
my $regex = qr/
\/ # A literal slash followed by
( # Capture to $1 ...
[^\/]+ # ... anything that's not a slash
) # close capture to $1
@@ # Must be immdiately followed by literal @@
/x;
my %line_num ;
while (<>) {
next unless /$regex/ ;
my $pattern = $1 ;
if ( $line_num{ $pattern } ) {
say "'$pattern' appears on lines ", $line_num{ $pattern }, " and $." ;
next ;
}
$line_num{ $pattern } = $. ; # Record the line number
}
# Ran on data above will produce;
# '.cdtproject' appears on lines 7 and 8
# '.cdtbuild' appears on lines 10 and 11