2014-09-19 32 views
0

行我有一个文件的内容:是:从文件中读取视病情

Change sets: 
    (0345) ---$User1 "test12" 
    Component: (0465) "textfiles1" 
    Modified: 14-Sep-2014 02:17 PM 
    Changes: 
     ---c- (0574) /<unresolved>/sha.txt 
    Work items: 
     (0466) 90516 "test defect 

我想读“90516”即工作项的使用Perl和放置在数组中的ID。 注意:这是一个条目,文件中可以有多行。 我想捕获所有这样的工作项目标识符,并将其放置在perl中。 代码

$file = new IO::File; 
$file->open("<sha.log") or die "Cannot open sha.log"; 
@file_list = <$file>; 
$file->close; 
my %seen; 
foreach $line (@file_list) { 
    #clear the array 
    undef %seen; 
    while ($line =~ m/Work items:/g) { 
     @temp = split(/[:|,]/, $1); 
     #push the item to array only if no items in temp array i.e. if the occurance is for the first time 
     next if $seen{ $temp[0] }++; 
     push @work_items, $temp[0]; 
    } 
} 
+1

显示你的代码。 – edem 2014-09-19 10:17:49

回答

0

使用Range operator ..

use strict; 
use warnings; 
use autodie; 

#open my $fh, '<', 'sha.log'; 
my $fh = \*DATA; 

my @work_items; 

while (<$fh>) { 
    if (my $range = /Work items:/ ... !/^\s*\(\d+\) (\d+)/) { 
     push @work_items, $1 if $range > 1 && $range !~ /E/; 
    } 
} 

print "@work_items\n"; 

__DATA__ 
Change sets: 
    (0345) ---$User1 "test12" 
    Component: (0465) "textfiles1" 
    Modified: 14-Sep-2014 02:17 PM 
    Changes: 
     ---c- (0574) /<unresolved>/sha.txt 
    Work items: 
     (0466) 90516 "test defect 
     (0467) 90517 "test defect 
Change sets: 
    (0345) ---$User1 "test12" 
    Component: (0465) "textfiles1" 
    Modified: 14-Sep-2014 02:17 PM 
    Changes: 
     ---c- (0574) /<unresolved>/sha.txt 
    Work items: 
     (0468) 90518 "test defect 

输出:

90516 90517 90518 
+0

如果我想让该条目在数组中独一无二,该怎么办?应该没有重复的工作项目。 – user3616128 2014-09-22 07:45:30

+0

[perlfaq4 - 如何从列表或数组中删除重复元素?](http://perldoc.perl.org/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-或阵列%3f)的 – Miller 2014-09-22 14:52:18