在Perl中,当您需要使用字符串作为数据结构中的键时,您正在寻找HASH
内置类型,由%
sigil指定。 Perl散列的一个很好的特性是你不必预先声明一个复杂的数据结构。你可以使用它,Perl会根据这种用法推断出结构。
my @file = qw(ipax3_2011_01_27.txt ipax3_2011_02_01.txt ipax3_2011_02_03.txt);
my %ipax3;
for (@file) {
if (/^ipax3_(\d{4}_\d{2})_(\d{2}).txt$/) {
$ipax3{$1}{$2} = $_
}
else {
warn "bad file: $_\n"
}
}
for my $year_month (keys %ipax3) {
my $days = keys %{ $ipax3{$year_month} };
if ($days > 1) {
print "$year_month has $days files\n";
}
else {
print "$year_month has 1 file\n";
}
}
它打印:
2011_01 has 1 file
2011_02 has 2 files
要获取单个文件:
my $year_month = '2011_02';
my $day = '01';
my $file = $ipax3{$year_month}{$day};
上面我用了keys
既充当列表的返回值来遍历和作为天数。这是可能的,因为keys
将返回列表上下文中的所有键,并且将返回标量上下文中的键数。上下文是由周围的代码提供:
my $number = keys %ipax3; # number of year_month entries
my @keys = keys %ipax3; # contains ('2011_01', '2011_02')
my @days = keys %{ $ipax{$year_month} };
在最后一个例子中,每个%ipax
值是散列的引用。由于keys
需要字面散列,因此您需要将$ipax{$year_month}
换算为%{ ... }
。在perl v5.13.7 +中,你可以省略参数%{ ... }
到keys
以及其他一些数据结构访问函数。
我试过了:#在数组元素中搜索模式“year_month”;# $ search = $ year。“_”。$ this_month; @match = grep(/ $ search/i,@file); 但这只会查找当前月份的任何文件。我需要在我的目录中查找任何具有两个或更多条目的文件。 – 2011-02-16 01:45:33