Perl编程:哈希处理与子例程应用
哈希处理
在Perl编程中,哈希是一种非常有用的数据结构,它可以将数据与单词关联起来。我们可以使用 while 或 foreach 循环来处理哈希中的每个键值对。
下面是一个名为 genes 的程序示例,它对一个哈希进行了两次处理,展示了这两种循环机制:
#! /usr/bin/perl -w
# The ’genes’ program - a hash of gene counts.
use constant
LINE_LENGTH => 60;
%gene_counts = (
Human => 31000,
’Thale cress’ => 26000,
’Nematode worm’ => 18000,
’Fruit fly’ => 13000,
Yeast => 6000,
’Tuberculosis microbe’ => 4000
);
print ’-’ x LINE_LENGTH, "\n";
while ( ( $genome, $count ) = each %gene_counts )
{
print "‘$genome’ has a gene count of $count\n";
}
print ’-’ x LINE_LENGTH, "\n";
foreach $genome ( sort keys %gene_counts )
{
print "‘$genome
超级会员免费看
订阅专栏 解锁全文
716

被折叠的 条评论
为什么被折叠?



