【问题标题】:Perl: Split Loop for a Large txt FilePerl:大型 txt 文件的拆分循环
【发布时间】:2014-02-07 13:30:51
【问题描述】:

我正在尝试使用 perl 拆分 txt 文件的内容以提取相关信息,但我似乎无法正确格式化我的拆分语句。文件中的一行如下所示:

1.8|8|28|98|1pter-p22.1|SAI1, MTS1, TFS1|C|Suppression of anchorage independence-1 (malignant transformation suppression-1)|154280|S, H|||4(Tfs1)|

我的拆分语句如下所示:

my $file;
my $important;
my $line;
my @info;
foreach $line (@OMIMFile) {
my ($noSystem, $month, $day, $year, $cytoLoc, $geneSymb
    , $geneStat, $title, $mimNo, $method, $comment, $disorder
    , $mousCorr, $ref) = split ("|", $line);
    $important = $geneSymb.":".$disorder;
    push @info, $important if ($geneStat =~ /C/i);
}

我希望@info 的每一行都是由冒号分隔的 $geneSymb 和 $disorder 的组合,但目前@info 的打印不返回任何内容,并且 $important 返回如下输出:

|:||:|2:7|:|1:||:|0:||:|0:|1:85:|7:|9:01:37:93:93:93:92:71: .....

我哪里错了?

提前致谢!

【问题讨论】:

  • 你忘记转义管道了:\|。它也应该是一个正则表达式:/\|/.

标签: perl split


【解决方案1】:

这是一个修复。

您的一些变量不是本地变量。

my $file;
my @info;
foreach my $line (@OMIMFile) {
my ($noSystem, $month, $day, $year, $cytoLoc, $geneSymb
    , $geneStat, $title, $mimNo, $method, $comment, $disorder
    , $mousCorr, $ref) = split (/\|/, $line);
    my $important = $geneSymb.":".$disorder;
    push @info, $important if ($geneStat =~ /C/i);
}

【讨论】:

  • 感谢您的帮助:)
【解决方案2】:

使用散列切片会更整洁:

my @fields = qw/ noSystem month day year cytoLoc geneSymb geneStat title mimNo method comment disorder mousCorr ref /;
my @info;

for (@OMIMFile) {
  my %line;
  @line{@fields} = split /\|/;
  my $important = join ':', @line{ qw/ geneSymb disorder / };
  push @info, $important if $line{geneStat} =~ /C/i
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2017-05-10
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多