我想存储一个数据的ini文件。如何在 perl 中存储立方体方法。
我试过了:
样式表.ini:
p indent noindent
h1 heading1
h2 heading2
h3 heading3
h4 heading4
h5 heading5
h6 heading6
disp-quote blockquote
脚本:
my %stylehash;
open(INI, 'stylesheet.ini') || die "can't open stylesheet.ini $!n";
my @style = <INI>;
foreach my $sty (@style){
chomp($sty);
split /t/, $sty;
$stylehash{$_[0]} = [$_[1], $_[2], $_[3], $_[4]];
}
print $stylehash{"h6"}->[0];
在这里,我分配 $[2]、$[3]、$_[4] 个不需要的数组,因为第一个 P 标签将得到两个数组,然后 h1 得到一个数组。我怎样才能完美地存储以及如何检索它。
我需要:
$stylehash{$_[0]} = [$_[1], $_[2]]; #p tag
$stylehash{$_[0]} = [$_[1]]; #h1 tag
print $stylehash{"h1"}->[0];
print $stylehash{"p"}->[0];
print $stylehash{"p"}->[1];
如何存储多维数据集方法。标签始终是唯一的,样式名称随机增加或减少。我该如何解决这个问题。
如果我理解正确,你有一堆带有值列表的键。 也许一个值,也许两个,也许三个...你想存储这个。 最简单的方法是将其构建到列表哈希中,并使用预先存在的数据格式,如JSON,可以很好地处理Perl数据结构。
use strict;
use warnings;
use autodie;
use JSON;
# How the data is laid out in Perl
my %data = (
p => ['indent', 'noindent'],
h1 => ['heading1'],
h2 => ['heading2'],
...and so on...
);
# Encode as JSON and write to a file.
open my $fh, ">", "stylesheet.ini";
print $fh encode_json(%data);
# Read from the file and decode the JSON back into Perl.
open my $fh, "<", "stylesheet.ini";
my $json = <$fh>;
my $tags = decode_json($json);
# An example of working with the data.
for my $tag (keys %tags) {
printf "%s has %sn", $tag, join ", ", @{$tags->{$tag}};
}
有关使用数组哈希的更多信息。