从在Perl文件中读取节 [英] Reading sections from a file in Perl

查看:112
本文介绍了从在Perl文件中读取节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Perl中输入文件读取值。
输入文件如下:

I am trying to read values from an input file in Perl. Input file looks like:

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 进入 @数组1 和数据 2 sampledata2 @数组2 去等。
我有这样的约50节。像 50 sampledata50

I want to read the above data so that data for 1-sampledata1 goes into @array1 and data for 2-sampledata2 goes in @array2 and so on. I will have about 50 sections like this. like 50-sampledata50.

修改:名称不会永远的X sampledataX。我只是做了例如。所以名字不能是一个循环。我想我要手动输入他们

EDIT: The names wont always be X-sampledataX. I just did that for example. So names cant be in a loop. I think I'll have to type them manually

我到目前为止有以下的(工作)。但是我正在寻找一种更有效的方式来做到这一点。

I so far have the following (which works). But I am looking for a more efficient way to do this..

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/);
        }
}

正如你可以看到我为每个部分,并针对不同的每个部分循环不同的阵列。如果我有办法去我迄今然后我将结束50圈和50阵列。

As you can see I have different arrays for each section and different for loops for each section. If I go with approach I have so far then I will end up with 50 for loops and 50 arrays.

有另一种更好的方式来做到这一点?最后我确实想用50阵列来结束,但不想写50环。而且,由于我将通过50阵列后来在程序循环,也许它们存储在一个数组?我是新来使用Perl那么它有点压倒...

Is there another better way to do this? In the end I do want to end up with 50 arrays but do not want to write 50 for loops. And since I will be looping through the 50 arrays later on in the program, maybe store them in an array? I am new to Perl so its kinda overwhelming ...

推荐答案

首先要注意的事情是,你正在试图用整数后缀使用变量名:不要。无论何时你发现你自己想要做的是使用数组。其次,你只需要读取多次去了文件的内容一次,而不是。第三,通常没有很好的理由在Perl对待一个字符串的字符数组。

The first thing to notice is that you are trying to use variable names with integer suffixes: Don't. Use an array whenever you find your self wanting to do that. Second, you only need to read to go over the file contents once, not multiple times. Third, there is usually no good reason in Perl to treat a string as an array of characters.

更新:此code版本使用前导空格的存在,以决定该怎么做。我离开了previous版本升级以及供参考。

Update: This version of the code uses existence of leading spaces to decide what to do. I am leaving the previous version up as well for reference.

#!/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

previous版本:

#!/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

这篇关于从在Perl文件中读取节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆