固定寬度表示unpack
給我。可以用正則表達式和分割進行解析,但unpack
應該是更安全的選擇,因爲它是固定寬度數據的正確工具。
我把第一個字段的寬度設置爲12,將空白間隔設置爲13,這對於這些數據起作用。你可能需要改變它。模板"A12A13A*"
的意思是「找到12,然後13個ASCII字符,後面是任意長度的ASCII字符」。 unpack
將返回這些匹配的列表。另外,如果沒有提供字符串,unpack
將使用$_
,這是我們在這裏執行的操作。
請注意,如果第一個字段的寬度不是固定的,因爲它看起來在您的示例數據中,您需要合併模板中的字段,例如, 「A25A *」,然後剝離結腸。
我選擇數組作爲存儲設備,因爲我不知道您的字段名是否是唯一的。哈希將覆蓋具有相同名稱的字段。數組的另一個好處是它保留了數據在文件中出現的順序。如果這些東西無關緊要,而且快速查找更重要,則可以使用散列代替。
代碼:
use strict;
use warnings;
use Data::Dumper;
my $last_text;
my @array;
while (<DATA>) {
# unpack the fields and strip spaces
my ($field, undef, $text) = unpack "A12A13A*";
if ($field) { # If $field is empty, that means we have a multi-line value
$field =~ s/:$//; # strip the colon
$last_text = [ $field, $text ]; # store data in anonymous array
push @array, $last_text; # and store that array in @array
} else { # multi-line values get added to the previous lines data
$last_text->[1] .= " $text";
}
}
print Dumper \@array;
__DATA__
field name 1: Multiple word value.
field name 2: Multiple word value along
with multiple lines.
field name 3: Another multiple word
and multiple line value
with a third line
輸出:
$VAR1 = [
[
'field name 1:',
'Multiple word value.'
],
[
'field name 2:',
'Multiple word value along with multiple lines.'
],
[
'field name 3:',
'Another multiple word and multiple line value with a third line'
]
];
來源
2011-12-15 00:08:28
TLP
謝謝!我將`。+`的第一個實例更改爲`。+?`,以使模式匹配不成立。這幫助我使用包含「:」字符的值。 – NeonD 2011-12-14 21:44:38