perl入门之---哈希的应用

在此发下个人广告,见谅,有兴趣的朋友可以看下:http://www.wachnew.com

 

#!/usr/bin/perl -w

$family_name{"fred"} = "flinkstone";   #给哈希键赋值
$family_name{"barney"} = "rubble";


foreach my $person (qw /fred barney/ )  #将私有变量声明写到foreach里边
{
    print "the $person is : $family_name{$person} /n";  #输出键及所对应的键值
}

my %hash = (                            #创建哈希,并给相应用键/值对赋值
    "a" => 1,
    "b" => 2,
    "c" => 3,
    "d" => 4,
    "e" => 5,
);

my @k = keys %hash;  #提取出哈希键列表
my @v = values %hash; #提取出哈希值列表
my $count = keys %hash;  #提取哈希健值对数
print "@k/n"."@v/n","number = $count/n";

if( %hash )#若哈希不为空则为真
{
    print "OK/n";
}

my %test;
if( %test )
{
    print "OK for test/n";
}
else
{
    print "false for test/n";
}

while( ($key, $value) = each %hash )
{
    print "$key => $value/n";
}


#对哈希键进行排序
print "/n";
foreach $key (sort keys %hash)  #对哈希中的键取到后进行排列,逐一放入value标量中
{
    print "$key => $hash{$key}/n";
}

#获取系统环境变量值
print "the PATH is $ENV{PATH} /n";   #获取系统路径地址

你可能感兴趣的:(c,perl,Path,each)