perl 中 hash用法请教
本帖最后由 fibbery 于 2012-12-26 09:03:27 编辑 有如下代码:
open RULE,"< switch" or die "ERROR: Can't open file switch\n";
while(<RULE>){
chomp();
if($_ =~ /^\s*#DEFINE\s*(\S+)\s+(.*)(\d+)\~(\d+)/i){
$NAME = $1;
for($3 .. $4){
push $switch1->@options1, "$_";
}
%switch1=(
"SWITCHN" => $NAME,
"OPTION" => \@options1
);
push @switchs, \%switch1;
}
}
open RULE, "< switch" or die "ERROR: Can't open file switch\n";
my %switch1;
while( <RULE> )
{
chomp();
if( $_ =~ /^\s*#DEFINE\s*(\S+)\s+(.*)(\d+)\~(\d+)/i )
{
$NAME = $1;
my @options1;
for( $3 .. $4 )
{
push @options1, $_;
}
$switch1{$NAME} = \@options1;
}
}
for my $key ( keys %switch1 )
{
print $key, ":";
my $array = $switch1{$key};
for my $item ( @$array )
{
print "$item ";
}
print "\n";
}