Skip to Content
Perl语言入门
book

Perl语言入门

by Randal L. Schwartz, brian d foy, Tom Phoenix
August 2018
Beginner to intermediate
382 pages
7h 14m
Chinese
Southeast University Press
Content preview from Perl语言入门
82
4
sub which_element_is {
my($what, @array) = @_;
foreach (0..$#array) { #
数组
@array
中所有元素的索引
if ($what eq $array[$_]) {
return $_; #
一发现就提前返回
}
}
-1; #
表示没找到符合条件的元素(可以省略
return
}
我们希望这个子程序能找出
@names
数组中值为
dino
的元素的索引。首先,用
my
明参数变量:一个是
$what
,表示要搜索的值;另一个是
@array
,表示要搜索的目
标数组。请看上面的代码,这个数组其实是
@names
的拷贝。
foreach
循环依此取出
@array
的索引(第一个是
0
,最后一个是
$#array
,这个已经在第
3
章介绍过了)。
每次执行
foreach
循环时,会检查
$what
中的字符串是否等于位于数组
@array
前索引中的元素。如果相等,立即返回当前索引值。这是关键字
return
最常见的用
法:立即返回某个值,而不再执行子程序的其余部分。
但如果找不到符合条件的元素呢?本例以返回
1
作为“查无此项”的数字代号。其
实返回
undef
更符合
Perl
的风格(
Perlish
),但这段代码的作者还是决定用
–1
。最
后一行写成
return1
也行,但因为这是最后一行,可以省略
return
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

计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩
Perl语言入门(第8版)

Perl语言入门(第8版)

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787564177911