バックスラッシュ継続行の処理を含むほとんどのテキスト処理は、Perl では非常に単純です。必要なのは、このような読み取りループだけです。
while (<>) {
$_ .= <> while s/\\\n// and not eof;
}
以下のプログラムは、私があなたが望むと思うことを行います。print
継続行にわたって集約された完全なレコードを表示するために、読み取りループに呼び出しを入れました。b1
また、例として挙げたフィールドを抽出する方法を示し、Data::Dump
作成されたデータ構造を確認できるように出力を示しました。
use strict;
use warnings;
my %data;
while (<DATA>) {
next if /^#/;
$_ .= <DATA> while s/\\\n// and not eof;
print;
chomp;
my ($key, $values) = split /=/;
my @values = map [ split /:/ ], split /,/, $values;
$data{$key} = \@values;
}
print $data{Property1}[1][1], "\n\n";
use Data::Dump;
dd \%data;
__DATA__
##
## Start of property1
##
##
Property1=\
a:b,\
a1:b1,\
a2,b2
##
## Start of propert2
##
Property2=\
c:d,\
c1:d1,\
c2,d2
出力
Property1=a:b,a1:b1,a2,b2
Property2=c:d,c1:d1,c2,d2
b1
{
Property1 => [["a", "b"], ["a1", "b1"], ["a2"], ["b2"]],
Property2 => [["c", "d"], ["c1", "d1"], ["c2"], ["d2"]],
}
アップデート
あなたの質問をもう一度読みましたが、データの別の表現を好むかもしれません。このバリアントは、プロパティ値を配列の配列ではなくハッシュとして保持します。それ以外の場合、その動作は同じです
use strict;
use warnings;
my %data;
while (<DATA>) {
next if /^#/;
$_ .= <DATA> while s/\\\n// and not eof;
print;
chomp;
my ($key, $values) = split /=/;
my %values = map { my @kv = split /:/; @kv[0,1] } split /,/, $values;
$data{$key} = \%values;
}
print $data{Property1}{a1}, "\n\n";
use Data::Dump;
dd \%data;
出力
Property1=a:b,a1:b1,a2,b2
Property2=c:d,c1:d1,c2,d2
b1
{
Property1 => { a => "b", a1 => "b1", a2 => undef, b2 => undef },
Property2 => { c => "d", c1 => "d1", c2 => undef, d2 => undef },
}