3

我正在尝试在 perl 中处理文本文件。我需要将文件中的数据存储到数据库中。我遇到的问题是某些字段包含换行符,这让我有点失望。包含这些字段的最佳方法是什么?

示例 data.txt 文件:

ID|Title|Description|Date
1|Example 1|Example Description|10/11/2011
2|Example 2|A long example description
Which contains
a bunch of newlines|10/12/2011
3|Example 3|Short description|10/13/2011

当前(损坏的)Perl 脚本(示例):

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

open (MYFILE, 'data.txt');
while (<MYFILE>) {
    chomp;
    my ($id, $title, $description, $date) = split(/\|/);

    if ($id ne 'ID') {
        # processing certain fields (...)

        # insert into the database (example)
        $sqlInsert->execute($id, $title, $description, $date);
    }
}
close (MYFILE);

从示例中可以看出,在 ID 2 的情况下,它被分成几行,在尝试引用那些未定义的变量时导致错误。您将如何将它们分组到正确的字段中?

提前致谢!(我希望问题足够清楚,很难定义标题)

4

3 回答 3

5

我只会在拆分行之前计算分隔符的数量。如果您没有足够的内容,请阅读下一行并附加它。tr运算符是一种计算字符数的有效方法。

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

open (MYFILE, '<', 'data.txt');
while (<MYFILE>) {
    # Continue reading while line incomplete:
    while (tr/|// < 3) {
        my $next = <MYFILE>;
        die "Incomplete line at end" unless defined $next;
        $_ .= $next;
    }

    # Remaining code unchanged:
    chomp;
    my ($id, $title, $description, $date) = split(/\|/);

    if ($id ne 'ID') {
        # processing certain fields (...)

        # insert into the database (example)
        $sqlInsert->execute($id, $title, $description, $date);
    }
}
close (MYFILE);
于 2011-05-20T17:29:14.790 回答
0

阅读下一行,直到您需要的字段数。类似的东西(我没有测试过那个代码):

my @fields = split(/\|/);
unless ($#fields == 3) { # Repeat untill we get 4 fields in array

  <MYFILE>; # Read next line      
  chomp;

  # Split line
  my @add_fields = split(/\|/); 

  # Concatenate last element of first line with first element of the current line
  $fields[$#fields] = $fields[$#fields] . $add_fields[0]; 

  # Concatenate remaining array part
  push(@fields, @add_fields[1,$#add_fields]);

}
于 2011-05-20T17:19:33.590 回答
0

如果您可以更改 data.txt 文件以在每行/记录中包含管道分隔符作为最后一个字符,您可以在整个文件中啜饮,直接拆分为原始字段。然后,此代码将执行您想要的操作:

#!/usr/bin/perl
use strict;
use warnings;

my @fields;
{
  $/ = "|";
  open (MYFILE, 'C:/data.txt') or die "$!";
  @fields = <MYFILE>;
  close (MYFILE);

  for(my $i = 0; $i < scalar(@fields); $i = $i + 4) {
    my $id = $fields[$i];
    my $title = $fields[$i+1];
    my $description = $fields[$i+2];
    my $date = $fields[$i+3];
    if ($id =~ m/^\d+$/) {
        # processing certain fields (...)

        # insert into the database (example)
    }
  }
}
于 2011-05-20T18:04:44.320 回答