通过一个实例复习哈希和正则表达式:
功能:打开一个文件test.log,筛选出其中的日期和以.dat为结尾的字段,将日期小于给定日期的字段选出,其中相同日期的字段打印时日期只打印一次。
#!/usr/bin/perl -w
use 5.010;
use strict;
use Getopt::Long;
our ($date,$hash_date);
my $my_result = GetOptions( "date|d=s" => \$date );
unless ($my_result) {
&usage();
exit;
}
sub processfile {
my %date_typeOfDocument;
my $hash_values;
open TXT, 'test.log' or die $!;
while (<TXT>) {
if (m{\s*(\d+)/(.*)\.dat\Z}i) {
if ( $1 < $date ) {
$hash_date = $1;
if ( $2 =~ /(\w+-\d+)-\d+/ ) {
# print $hash_date," ",$1,"\n";
#$hash_values is a reference of hash table
#push the $1 into the value of hash table,the value is an array.
push @{$hash_values->{$hash_date}}, $1;
}
#The array is value of %date_typeOfDocument,and the array must be stored as a reference.we can use '\' realize it.
$date_typeOfDocument{$hash_date} = \@{$hash_values->{$hash_date}};
}
}
}
close(TXT);
while ( my ( $keys, $values ) = each %date_typeOfDocument ) {
print $keys,"\n";
#$values is a reference,if we want to traverse it,we should use it by @$values .
foreach my $print_values (@$values) {
my $val= pack "A12 A12","",$print_values;
print $val, "\n";
}
}
}
&processfile();
sub usage {
print <<EOF
$0 -d date;
EOF
}
总结:这里用到了哈希表,哈希表的key值为日期,value值是一个数组,即每个日期对应的所有字段。
其中 push @{$hash_values->{$hash_date}}, $1;表示:
$hash_values表示哈希的引用(指针),将$1作为$hash_date对应的value值,最后将每个$hash_date对应的值作为数组元素
注意:要想实现一个hash,如下形式:
%hash_array=
(
key1->[1,2,3],
key2->'hello world',
key3->[4,5]
)
实现它的方法就是push @{hash_ref->{hash_key}},hash_value即可