[英]Perl - reading in multi-line records from config file
I'm trying to read in a multi-line config file with records into a perl hash array 我正在尝试读取包含记录的多行配置文件到perl哈希数组中
Example Config File: 配置文件示例:
record_1
phone=5551212
data=1234234
end_record_1
record_2
people_1=bob
people_2=jim
data=1234
end_record_2
record_3
people_1=sue
end_record_3
here's what I'm looking for: 这是我在寻找的东西:
$myData{1}{"phone"} <--- 5551212
$myData{1}{"data"} <--- 1234234
$myData{2}{"people_1"} <--- bob
... etc
What's the best way to read this in? 阅读此内容的最佳方法是什么? Module?
模块? Regex with multi-line match?
正则表达式与多行匹配? Brute force?
蛮力? I'm up in the air on where to head next.
我不知道接下来要去哪里。
Here's one option with your data set: 这是您的数据集的一种选择:
use strict;
use warnings;
use Data::Dumper;
my %hash;
{
local $/ = '';
while (<DATA>) {
my ($rec) = /record_(\d+)/;
$hash{$rec}{$1} = $2 while /(\S+)=(.+)/g;
}
}
print Dumper \%hash;
__DATA__
record_1
phone=5551212
data=1234234
end_record_1
record_2
people_1=bob
people_2=jim
data=1234
end_record_2
record_3
people_1=sue
end_record_3
Output: 输出:
$VAR1 = {
'1' => {
'data' => '1234234',
'phone' => '5551212'
},
'3' => {
'people_1' => 'sue'
},
'2' => {
'people_1' => 'bob',
'data' => '1234',
'people_2' => 'jim'
}
};
Setting local $/ = ''
results in an empty line being treated as a "record separator" in your data set, so we can use regexs on those records to grab the information for the hash keys/values. 设置
local $/ = ''
会将空行视为数据集中的“记录分隔符”,因此我们可以在这些记录上使用正则表达式来获取哈希键/值的信息。
Hope this helps! 希望这可以帮助!
There are a number of modules for this, so the best practice is (as usual) to use them rather than re-invent the wheel. 为此有许多模块,因此最佳实践是(照常)使用它们而不是重新发明轮子。
From the snippet of the config file you posted, it looks like Config::Simple
may be the best choice. 从您发布的配置文件的片段中,看起来
Config::Simple
可能是最佳选择。 If you can simplify the config format, then Config::Tiny
would be easier to use. 如果您可以简化配置格式,则
Config::Tiny
会更易于使用。 If things get more complicated, then you may have to use Config::General
. 如果事情变得更加复杂,那么您可能必须使用
Config::General
。
http://metacpan.org/pod/Config::Tiny http://metacpan.org/pod/Config::Tiny
http://metacpan.org/pod/Config::Simple http://metacpan.org/pod/Config ::简单
http://metacpan.org/pod/Config::General http://metacpan.org/pod/Config::General
Read it in one line at a time. 一次一行阅读。
When you see a new record, add a new empty associative array to myData
and grab a reference to it - this will be your "current record". 当您看到新记录时,将新的空关联数组添加到
myData
并获取对其的引用-这将是您的“当前记录”。
Now when you see key/value pairs on a line, you can add that to the current record array (if there is one) 现在,当您在一行上看到键/值对时,可以将其添加到当前记录数组中(如果有的话)
When you see the end of a record, you just clear the reference to the current record. 当您看到记录的结尾时,您只需清除对当前记录的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.