'keep looping' while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/;
所以,现在我们知道替换运算是循环的真正目的。但此处的替换运算又做了什么事?
别忘了,这里的
$number
是个像
"12345678.90"
这样的字符串。上面的模式会匹配
字符串的第一个部分,但它不会越过小数点(你知道为什么吗?)。内存变量
$1
会
是
"12345"
,而
$2
会是
"678"
,所以这个替换运算会让
$number
变成
"12345,678.90"
(别忘了,它不会匹配小数点,所以字符串后面的部分并不会改变)。
你知道模式开头的减号是什么用处么?(提示:这个减号只能在字符串中的一个地方
出现),在本节结束时,万一你还没找到答案,可以看看我们的解答。
整个替换过程并非到此为止。既然替换运算成功了,这个无事可做的循环就会再来一
次。因为这次模式只能替换逗号之前的部分,所以
$number
会变成
"12,345,678.90"
。
于是在每次循环执行后,替换运算就会在数字中加一个逗号。
循环的工作还没完呢。因为上一次替换成功了,所以又会重新开始循环。但由于模式 ...
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.
O’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
I 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
I’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
I'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.