Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

the file is formated in this way:

[group1]
value1
value2
value3

[group2]
value1
value2
value3
value4

[group3]
value1
value2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
669 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...