3

我有一个类似的文件

line1
line2
line3
line4
.endm
line5
line6
line7
line8
.endm

我想将此文件拆分为 .endm 并将内容放入 2 个不同的数组中。输出将是

@array1=(line1,line2,line3,line4);
@array2=(line4,line6,line7,line8);

我尝试使用

@array = split(".endm",my_file.txt);
print("Array1 = $array[1]\nArray2 = $array[2]\n);

但这只是打印出最后一个 .endm 的行号

我也尝试过类似的东西

#! /usr/intel/bin/perl -w
use strict;

my $count=0;
open(my $fh, "<", "a.txt")|| die "can't open UTF-8 encoded filename: $!";
my @lines = <$fh>;
my @array;

LOOP: foreach my $line (@lines) {
    chomp ($line);
    $count = $count+1;
    push (@array,$line);  
    if ($line =~ m/.endm/) {
        pop (@array);
        last LOOP;
    }
}
print("Array : @array\n");
close $fh;

这个打印@array=(line1,line2,line3,line4);但我仍然需要其他 4 行,以便我可以将它放在不同的数组中

那么如何根据某个关键字来拆分文件呢?

4

6 回答 6

5

如果你有一个固定的字符串作为字段分隔符,你可以设置内置变量$/——输入记录分隔符——来改变 Perl 的readline()函数读取行的方式。

perl -MData::Dumper -we '$/=".endm\n"; chomp(@a = <>); 
      @a = map [ split /\n/ ], @a; print Dumper \@a;' file.txt

这将分两块读取文件,有效地分割.endm\n. 请注意,从字符串末尾chomp删除值。$/此处的map语句只是将输入拆分为换行符并将其放入数组 ref 中。结果数组是二维的,如Data::Dumper输出所示:

输出:

$VAR1 = [
          [
            'line1',
            'line2',
            'line3',
            'line4'
          ],
          [
            'line5',
            'line6',
            'line7',
            'line8'
          ]
        ];

完整版如下所示:

use strict;
use warnings;
use Data::Dumper;

$/ = ".endm\n";
chomp(my @a = <>);
@a = map [split /\n/], @a;
print Dumper \@a;
于 2013-09-20T19:36:18.240 回答
2
#!/usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;

open my $fh, '<', 'data.txt' or die "can't open data.txt: $!";

my @array;

$/ = ".endm";

while (<$fh>){
    s/\.endm$//;
    push @array, [split];
}

print Dumper(@array);

#output
$VAR1 = [
         'line1',
         'line2',
         'line3',
         'line4'
        ];
$VAR2 = [
         'line5',
         'line6',
         'line7',
         'line8'
        ];
于 2013-09-20T18:52:50.240 回答
2

我会保持简单并这样做:

my (@array1,@array2);
while ($_ = <>) { chomp; last if $_ eq '.endm'; push @array1, $_ }
while ($_ = <>) { chomp; last if $_ eq '.endm'; push @array2, $_ }
于 2013-09-20T18:43:31.003 回答
1

如果文件很小,这将起作用。

#!/usr/bin/perl 

use strict;
use warnings;

open(FILE,"input.txt");
my @lines =<FILE>;                 # Reads all lines of the file into an array
my $line  = join('',@lines);       # Join all lines together
my @temp = split(/.endm/,$line);   # Split the line using the keyword
my @array1=split(/\n/,$temp[0]);   # Splits line back 
my @array2=split(/\n/,$temp[1]);   # Splits line back

print @array1;
print "\n";
print @array2;

close(FILE);
于 2013-09-20T18:33:37.373 回答
1

在编写代码之前,您不会费心阅读文档或教程,对吗?拆分将字符串拆分为数组,而不是将文件拆分为行。

open INPUT,"<my_file.txt" or die "no such file";
my @array1;
my @array2;
my $ref = \@array1;
while ( my $line = <INPUT> ) {
  chomp($line);
  if ( $line eq ".endm" ) {
    $ref = \@array2;
  } else {
    push(@$ref,$line);
  }
}
print $_ . " " foreach @array1;
print "\n";
print $_ . " " foreach @array2;
于 2013-09-20T18:50:36.200 回答
1
use 5.10.0;
use strict;
use warnings;

open( my $fh, '<', 'a.txt' ) or die "can't open filename: $!";

my @arrays;
my $array = [];

while (<$fh>) {
    chomp;
    if (/^\.endm$/) {
        push @arrays, $array;
        $array = [];
        next;
    }
    push @$array, $_;
}

local $, = ",";
say @$_ for @arrays;

$array = []是不必要的,但可以提高可读性。并输出:

line1,line2,line3,line4
line5,line6,line7,line8
于 2013-09-20T18:45:41.707 回答