Skip to Content
Perl语言入门(第8版)
book

Perl语言入门(第8版)

by Randal L. Schwartz, brian d foy, Tom Phoenix
October 2022
Beginner to intermediate
390 pages
7h 5m
Chinese
Southeast University Press
Content preview from Perl语言入门(第8版)
哈希
119
题,它有良好、高效、可伸缩的算法。如果某个哈希只有三个键
-
值对,从中提取任
意一项数据当然都会非常快。如果某个哈希包含
300
万个键
-
值对,从中提取任意一
项数据还是会和原来一样快。所以,我们不用担心大数据量哈希的读写性能。
注意,虽然键是唯一的,但它们对应的值是可以重复的。哈希的值可以是数字、字符
串、
undef
,或是这些类型的组合。但哈希的键必须是字符串且不可重复。
为何使用哈希?
第一次听说哈希的人,尤其是多年从事其他语言开发的人,会奇怪为什么大家都想要
拥抱这个怪兽。其实哈希要解决的问题很常见,大家都需要将一组数据对应到另一组
数据。比如下面这些典型的应用场景:
驾驶执照号码,姓名
这个世界上有许许多多叫做
John Smith
的人,但每一个人的驾驶执照号码都应
该不同。我们可以把这个唯一的号码作为哈希的键,姓名作为对应的值。
单词,单词出现的次数
这是一个非常典型的哈希应用场景。本章末尾会以此为素材给出习题。
其实这个问题就是统计某篇文档中每个单词出现的频率,即词频。也许你在
为一堆文件编写索引,然后当用户搜索
fred
时,可以根据索引知道,某篇文
档提到
fred 5
次,另一篇文档提到
fred 7
次,并且还有篇文档一次都没提到
fred
。这样,我们就能知道用户想要的应该是哪篇文档了。索引构造程序
先通读给定文档,每次看到
fred
就把哈希中键为
fred
的值自增
1
。如果之前
已经出现过两次
fred
,那么就把原来的值
2
自增
1
后变为
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Perl语言入门

Perl语言入门

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787576602036