gpt4 book ai didi

linux - 读取 INI 文件 perl 中的新格式

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:24 25 4
gpt4 key购买 nike

我还有一个关于 INI 文件的问题。我的老师更新了文件的配置。现在我的代码无法读取这个新的配置文件。它的格式已更改。如何在perl中读取INI文件中的这种格式?

[section1]
value1
value2

如您所见,INI 文件的格式现在只有值。参数没了。 Perl 如何读取这一行?它没有任何参数,只留下。我只想读取。我之前使用 Config::Tiny 来读取该行,但我似乎无法解决这个问题:

my $file = "file directory";

my $Config = Config::Tiny->read($file);
$Config->{"section1"}->{_};

我的代码有错吗?因为我无法从这段代码中获取输出。有人能帮我解决这个问题吗?谢谢。

最佳答案

您需要自己处理:

#!/usr/bin/perl

use 5.10.0;

sub read_not_ini
{
my $file = shift;

my %values;
my $section;

open my $fh, '<', $file or die "Can't read $file: $!";
while (<$fh>)
{
# skip comments, blank lines.
next if /^\s*#/ or /^\s*$/;

# don't need/want end-line character.
chomp;

if (/^\[([^\]]+)\]/)
{
my $s = $1;
$section = $values{$s} //= [];
}
elsif($section)
{
push @$section, $_;
}
else
{
say STDERR "$file: values without a section: line $.";
}
}
return \%values;
}

my $Config = read_not_ini(shift); # pass in as param
say for @{$Config->{"section1"}};

关于linux - 读取 INI 文件 perl 中的新格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22827524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com