0

我正在尝试从 Perl 中的输入文件中读取值。输入文件如下所示:

1-sampledata1 This is a sample test
              and data for this continues
2-sampledata2 This is sample test 2
              Data for this also is on second line

我想读取上面的数据,以便数据1-sampledata1进入@array1和数据2-sampledata2进入@array2等等。我将有大约 50 个这样的部分。喜欢50-sampledata50

编辑:名称不会总是 X-sampledataX。例如,我只是这样做了。所以名字不能循环。我想我必须手动输入它们

到目前为止,我有以下(有效)。但我正在寻找一种更有效的方法来做到这一点..

foreach my $line(@body){
        if ($line=~ /^1-sampledata1\s/){
                $line=~ s/1-ENST0000//g;
                $line=~ s/\s+//g;
                push (@array1, $line);
          #using splitarray because i want to store data as one character each
          #for ex: i wana store 'This' as T H I S in different elements of array
                @splitarray1= split ('',$line);
        last if ($line=~ /2-sampledata2/);
        }
}
foreach my $line(@body){
        if ($line=~ /^2-sampledata2\s/){
                $line=~ s/2-ENSBTAP0//g;
                $line=~ s/\s+//g;
                @splitarray2= split ('',$line);
        last if ($line=~ /3-sampledata3/);
        }
}

如您所见,每个部分都有不同的数组,每个部分都有不同的 for 循环。如果我采用到目前为止的方法,那么我最终将得到 50 个 for 循环和 50 个数组。

还有另一种更好的方法吗?最后,我确实想得到 50 个数组,但不想写 50 个 for 循环。由于稍后我将在程序中循环遍历 50 个数组,也许将它们存储在一个数组中?我是 Perl 的新手,所以它有点压倒性......

4

4 回答 4

4

首先要注意的是您正在尝试使用带有整数后缀的变量名:不要。每当您发现自己想要这样做时,请使用数组。其次,您只需要阅读一次即可查看文件内容,而不是多次。第三,在 Perl 中通常没有充分的理由将字符串视为字符数组。

更新:此版本的代码使用前导空格的存在来决定要做什么。我也将保留以前的版本以供参考。

#!/usr/bin/perl

use strict;
use warnings;

my @data;

while ( my $line = <DATA> ) {
    chomp $line;
    if ( $line =~ s/^ +/ / ) {
        push @{ $data[-1] }, split //, $line;
    }
    else {
        push @data, [ split //, $line ];
    }
}

use Data::Dumper;
print Dumper \@data;

__DATA__
1-sampledata1 This is a sample test
              and data for this continues
2-sampledata2 This is sample test 2
              Data for this also is on second line

以前的版本:

#!/usr/bin/perl

use strict;
use warnings;

my @data;

while ( my $line = <DATA> ) {
    chomp $line;
    $line =~ s/\s+/ /g;
    if ( $line =~ /^[0-9]+-/ ) {
        push @data, [ split //, $line ];
    }
    else {
        push @{ $data[-1] }, split //, $line;
    }
}

use Data::Dumper;
print Dumper \@data;

__DATA__
1-sampledata1 This is a sample test
              and data for this continues
2-sampledata2 This is sample test 2
              Data for this also is on second line
于 2009-07-11T14:39:29.490 回答
1
#! /usr/bin/env perl
use strict;
use warnings;

my %data;
{
  my( $key, $rest );
  while( my $line = <> ){
    unless( ($rest) = $line =~ /^     \s+(.*)/x ){
      ($key, $rest) = $line =~ /^(.*?)\s+(.*)/;
    }
    push @{ $data{$key} }, $rest;
  }
}
于 2009-07-11T14:57:00.600 回答
1

下面的代码与@Brad Gilbert@Sinan Unur的解决方案非常相似:

#!/usr/bin/perl
use strict;
use warnings;    
use Data::Dumper;

my (%arrays, $label);
while (my $line = <DATA>) 
{
    ($label, $line) = ($1, $2) if $line =~ /^(\S+)(.*)/; # new data block

    $line =~ s/^\s+//; # strip whitespaces from the begining
    # append data for corresponding label
    push @{$arrays{$label}}, split('', $line) if defined $label;
}

print $arrays{'1-sampledata1'}[2], "\n";     # 'i'
print join '-', @{$arrays{'2-sampledata2'}}; # 'T-h-i-s- -i-s- -s-a-m-p-l
print Dumper \%arrays;

__DATA__
1-sampledata1 This is a sample test
              and data for this continues
2-sampledata2 This is sample test 2
              Data for this also is on second line

输出

i
T-h-i-s- -i-s- -s-a-m-p-l-e- -t-e-s-t- -2-D-a-t-a- -f-o-r- -t-h-i-s- -a-l-s-o- -i-s- -o-n- -s-e-c-o-n-d- -l-i-n-e-
$VAR1 = {
          '2-sampledata2' => [
                               'T',
                               'h',
                               'i',
                               's',
                               ' ',
                               'i',
                               's',
                               ' ',
                               's',
                               'a',
                               'm',
                               'p',
                               'l',
                               'e',
                               ' ',
                               't',
                               'e',
                               's',
                               't',
                               ' ',
                               '2',
                               'D',
                               'a',
                               't',
                               'a',
                               ' ',
                               'f',
                               'o',
                               'r',
                               ' ',
                               't',
                               'h',
                               'i',
                               's',
                               ' ',
                               'a',
                               'l',
                               's',
                               'o',
                               ' ',
                               'i',
                               's',
                               ' ',
                               'o',
                               'n',
                               ' ',
                               's',
                               'e',
                               'c',
                               'o',
                               'n',
                               'd',
                               ' ',
                               'l',
                               'i',
                               'n',
                               'e',
                               '
'
                             ],
          '1-sampledata1' => [
                               'T',
                               'h',
                               'i',
                               's',
                               ' ',
                               'i',
                               's',
                               ' ',
                               'a',
                               ' ',
                               's',
                               'a',
                               'm',
                               'p',
                               'l',
                               'e',
                               ' ',
                               't',
                               'e',
                               's',
                               't',
                               'a',
                               'n',
                               'd',
                               ' ',
                               'd',
                               'a',
                               't',
                               'a',
                               ' ',
                               'f',
                               'o',
                               'r',
                               ' ',
                               't',
                               'h',
                               'i',
                               's',
                               ' ',
                               'c',
                               'o',
                               'n',
                               't',
                               'i',
                               'n',
                               'u',
                               'e',
                               's',
                               '
'
                             ]
        };
于 2009-07-11T16:11:06.513 回答
0

相反,您应该使用哈希映射到数组。

使用此正则表达式模式获取索引:

/^(\d+)-sampledata(\d+)/

然后,用my %arrays,做:

push($arrays{$index}), $line;

然后,您可以使用$arrays{$index}.

于 2009-07-11T14:34:42.197 回答