1

I have a tab separated text file. I read line by line and column by column. I make few changes in each column and write the line to a new file. When I read each column using split function of perl

my @aLastOldElements = split(/\t/, $_);

I miss out on empty columns in the end. For example if file has 33 tab separated columns, out of which 10 in the end are empty. The split function creates array of size 23. I want to have all the columns. Because this way the header of file (33 columns) doesn't match the data (23 columns) and I get errors while writing the file to the database.

4

2 回答 2

4

split接受一个可选的第三个参数,用于返回的最大字段数。如果存在,则不会丢弃空的尾随字段:

perl -E '@arr = split(/ /, "foo bar            ", 100); say scalar @arr'
14

只要存在用于分隔行尾空字段的选项卡,即使最后 10 个字段为空,也应该始终在数组中为您提供 33 个字段。(在我的示例中,返回了 14 个字段,因为字符串包含 13 个分隔符,即使指定的限制为 100。)

编辑:回答第一条评论中的问题:

perl -wE '@arr = split(/\t/, "foo\tbar\t\thello\t", 100); say $_ || "(empty field)" for @arr'
foo
bar
(empty field)
hello
(empty field)
于 2013-05-10T13:11:32.677 回答
1

如果您知道这些列应该在那里,无论它们是否有任何数据,您都可以自己确保结果。

my @aLastOldElements = split(/\t/, $_);
my $short_fall       = 33 - @aLastOldElements;
if ( $short_fall > 0 ) {
    push @aLastOldElements => ( '' ) x $short_fall;
}
于 2013-05-10T13:12:34.683 回答