将文件存放到数组中

将文件存放到数组中

怎样把以下文件内容存放到数组中
要求如下:
1、首先忽略第一行的内容“### Layer - tmp_1d features data ###”从第二行开始
2、如果开始是#P,则将后面的数据:154.0322放到@x数组中、35.8378放到@y数组中、300放到@s数组中。如果开始是#L,则分别将数据放到@xs、@ys、@xe、@ye、@s中

### Layer - tmp_1d features data ###
#P 154.0322 35.8378 r300 P 1 0 N;.bit=0.30,.drill=via
#P 159.417 51.125 r300 P 1 0 N;.bit=0.30,.drill=via
#P 172.371 92.8608 r300 P 1 0 N;.bit=0.30,.drill=via
#P 154.845 35.8378 r300 P 1 0 N;.bit=0.30,.drill=via
#P 162.417 51.1248 r300 P 1 0 N;.bit=0.30,.drill=via
#P 171.101 93.2418 r300 P 1 0 N;.bit=0.30,.drill=via
#L 69.16305 290.8754 71.36295 290.8754 r960 P 4 ;.bit=0.90,.drill=plated
#L 65.4624 286.52425 65.4624 288.72415 r960 P 4 ;.bit=0.90,.drill=plated
#L 69.01294 284.627 71.51306 284.627 r1160 P 9 ;.bit=1.10,.drill=plated
有点乱啊

[Copy to clipboard] [ - ]
CODE:
$ cat file
### Layer - tmp_1d features data ###
#P 154.0322 35.8378 r300 P 1 0 N;.bit=0.30,.drill=via
#P 159.417 51.125 r300 P 1 0 N;.bit=0.30,.drill=via
#P 172.371 92.8608 r300 P 1 0 N;.bit=0.30,.drill=via
#P 154.845 35.8378 r300 P 1 0 N;.bit=0.30,.drill=via
#P 162.417 51.1248 r300 P 1 0 N;.bit=0.30,.drill=via
#P 171.101 93.2418 r300 P 1 0 N;.bit=0.30,.drill=via
#L 69.16305 290.8754 71.36295 290.8754 r960 P 4 ;.bit=0.90,.drill=plated
#L 65.4624 286.52425 65.4624 288.72415 r960 P 4 ;.bit=0.90,.drill=plated
#L 69.01294 284.627 71.51306 284.627 r1160 P 9 ;.bit=1.10,.drill=plated

$  perl -ane 'next if $. == 1; if ($F[0] eq "#P") { push @x, $F[1]; push @y, $F[2]; push @s, substr($F[3],1);} elsif ($F[0] eq "#L") { push @xs, $F[1]; push @ys, $F[2]; push @xe, $F[3]; push @ye, $F[4]; push @s, substr($F[5],1);}}{print "\@x:@x\n\@y:@y\n\@xs:@xs\n\@ys:@ys\n\@xe:@xe\n\@ye:@ye\n\@s:@s\n";' file
@x:154.0322 159.417 172.371 154.845 162.417 171.101
@y:35.8378 51.125 92.8608 35.8378 51.1248 93.2418
@xs:69.16305 65.4624 69.01294
@ys:290.8754 286.52425 284.627
@xe:71.36295 65.4624 71.51306
@ye:290.8754 288.72415 284.627
@s:300 300 300 300 300 300 960 960 1160

笨方法,不过很容易明白,呵呵。

my (@x,@y,@s,@xs,@ys,@xe,@ye);
while (<DATA>){
    if (/^#P\s+(\S+)\s+(\S+)\s+r(\S+)\s+/){

        push @x,$1;
        push @y,$2;
        push @s,$3;
    }
    elsif(/^#L\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+r(\S+)\s+/){

        push @xs,$1;
        push @ys,$2;
        push @xe,$3;
        push @ye,$4;
        push @s,$5;
    }
}

print "\@x:  " .join "\t",@x,"\n";
print "\@y:  " .join "\t",@y,"\n";
print "\@s:  " .join "\t",@s,"\n";
print "\@xs: " .join "\t",@xs,"\n";
print "\@ys: " .join "\t",@ys,"\n";
print "\@xe: " .join "\t",@xe,"\n";
print "\@ye: " .join "\t",@ye,"\n";

__DATA__

### Layer - tmp_1d features data ###

#P 154.0322 35.8378 r300 P 1 0 N;.bit=0.30,.drill=via

#P 159.417 51.125 r300 P 1 0 N;.bit=0.30,.drill=via

#P 172.371 92.8608 r300 P 1 0 N;.bit=0.30,.drill=via

#P 154.845 35.8378 r300 P 1 0 N;.bit=0.30,.drill=via

#P 162.417 51.1248 r300 P 1 0 N;.bit=0.30,.drill=via

#P 171.101 93.2418 r300 P 1 0 N;.bit=0.30,.drill=via

#L 69.16305 290.8754 71.36295 290.8754 r960 P 4 ;.bit=0.90,.drill=plated

#L 65.4624 286.52425 65.4624 288.72415 r960 P 4 ;.bit=0.90,.drill=plated


或者split,跟楼上一样处理数组。