本文介绍了以Perl解析INI(列表格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中解析ini文件的最佳方法是什么?

What is the best way to parse ini files in Perl?

文件的格式如下:

[group1]
value1
value2
value3

[group2]
value1
value2
value3
value4

[group3]
value1
value2

推荐答案

您尚未告诉我们数据的预期格式或未显示任何现有代码,因此无法知道您要查找的内容,但这应该至少让您达到90%的路程:

You haven't told us the expected format for the data or shown any existing code, so it's impossible to know what you're looking for, but this should get you at least 90% of the way there:

use strict;
use warnings;

use Data::Dumper;

my %config;
my $group = '';

while (<DATA>) {
    chomp;
    next unless /\S/;

    if (/^\[([^]]+)\]/) {
        $group = $1;
        next;
    }

    push(@{$config{$group}}, $_);
}

print Dumper(\%config);

__DATA__
[group1]
value1
value2
value3

[group2]
value1
value2
value3
value4

[group3]
value1
value2

这篇关于以Perl解析INI(列表格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:55