我的正則表達式只匹配一些表達式。當我在regex101.com上測試表達式時,它工作得很好......可能是我的代碼存在問題?perl:正則表達式只匹配一些時間
感謝您的幫助提前。
示例文件, 「surfacecoating」:
[
('amino acids', 339, 350),
('copper', 71, 77),
('copper', 0, 6),
('copper', 291, 297),
('amino acids', 119, 130)]
自卸車打印出這個文件是什麼(請注意前3場比賽不返回):
'surfacecoating' => {
'copper' => '291',
'amino acids' => '119'
},
代碼:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
determine_cde_instances();
sub determine_cde_instances {
my %cdeinstances;
my %cde_instances;
my $dir = "/results/CDE";
opendir my $dh, $dir or die "Can't open $dir: $!";
while (my $file = readdir($dh)) {
next if ($file =~ m/^\./);
next if -d $file;
open my $fh, '<', "$dir/$file" or die "Can't open $dir/$file: $!";
while (my $line = <$fh>)
{
if (my ($instance) = $line =~ m/'(.*?)', (.*?), /)
{
my $instance = $1;
my $pos = $2;
$cde_instances{$file}{$instance} = $pos;
}
}
close $fh;
}
close $dh;
print Dumper(\%cde_instances);
return %cde_instances;
}
您的數據文件是序列的集合,但是您的代碼會在序列的一個成員上鍵入一個(嵌套的)關聯數組。一個[數組數組](http://perldoc.perl.org/perldsc.html#ARRAYS-OF-ARRAYS)最明顯地代表了數據文件中的結構 - 但是你想在代碼中使用什麼結構?換句話說,你會期望'Dumper'輸出什麼? – pilcrow
您可能會在將每個'cde_instance'覆蓋到'$ cde_instances'的同時覆蓋每個'cde_instance'(它可能需要將第一個匹配作爲關鍵字[只是猜測],這與'('copper',0,6'和'('copper',291,297)'。在$ cde_instances {$ file} {$ instance} = $ pos;' –