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语言入门
100
5
printf "Monthly interest rate: %.2f%%\n",
5.25/12; #
数字部分显示结果为
"0.44%"
既然是转义,你大概会觉得在百分号前加个反斜线就行了。想法很好,但是不对。这
是因为首先是内插,带有反斜线的转义字符
"\%"
内插后的结果还是字符串
'%'
,然后
再给
printf
作为参数使用,这时候
printf
看到的是单个百分号,后续没有完整的格
式定义,它会困惑。
以上我们通过格式定义直接写明字段宽度,其实宽度也可以作为参数另外指定。先在
格式字符串中用
*
占位表示,接下来的第一个参数就是宽度,第二个是字符串内容:
printf "%*s", 10, "wilma"; #
输出
`````wilma
也可以用两个
*
表示浮点数的总宽度和小数部分的宽度:
printf "%*.*f", 6, 2, 3.1415926; #
输出
```3.14
printf "%*.*f", 6, 3, 3.1415926; #
输出
``3.142
以上都是最基础的用法,更多变化请阅读
perlfunc
文档的
<sprintf>
部分。
数组和
printf
一般来说,我们不会把数组当成参数给
printf
使用。因为数组可以包含任意数目的
元素,而格式字符串只用到固定数目的元素:如果写了三个转换格式,对应地就需要
三个元素,不多也不少。
但没人规定你不能在程序运行时动态产生格式字符串,它们可以是任意的表达式。要
想处理好需要一些技巧,比如把格式字符串存到变量,这样可能使用灵活,也方便调 ...
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