学习Perl-Perl教程
问题描述:
你好,
我决定学习一些Perl,并通过Nik Silver找到了本教程: http://www.comp.leeds.ac. uk/Perl/ [ ^ ].在控制结构练习中,我解决了第一部分:在每行之前放置一个行号,但是我不知道如何解决第二部分:在前面放置一个3位数字每行. 有人有什么想法吗?
真诚的
Mike
Hello,
I decided to learn some Perl and I found this tutorial by Nik Silver: http://www.comp.leeds.ac.uk/Perl/[^]. In the Control Structures exercise, I solved the first part: putting a line number before each line, but I don''t know how to solve the second part: putting a 3-digit number before each line. Does anyone have any ideas?
Sincerely,
Mike
答
如果您可以展示尝试过的方法,那会很好,但是如果没有这样做,我想您可能需要做的只是查找,并使用 %03d
之类的格式.假设您已经安装了不错的Perl,使用perldoc -f sprintf
应该可以在命令行中找到所需的所有内容.请注意,sprintf
和printf
使用相同的格式代码,如果您完全了解C,他们应该非常熟悉.
一种替代方法是使用Perl的format
工具,该工具功能非常强大,但在当今的HTML等中几乎未使用.过去,我已经使用它进行了一些漂亮的文本格式化,以生成报告,如果您使用的是简单文本,则可能值得一看.
欢呼!
Don
It would be good if you would show what you have tried, but in absence of that, I think all you likely need to do is look upprintf
, and use a format like%03d
. Assuming you have a decent Perl installation, you should be able to find out all you need at the command line withperldoc -f sprintf
. Note thatsprintf
andprintf
use the same formatting codes, and if you know C at all they should be very familiar.
An alternative would be to use Perl''sformat
facility which is quite powerful, but mostly unused in these days of HTML and so on. I have done some pretty fancy text formatting with it in the past for report generation, and if you''re working in simple text it may be worth a look.
cheers!
Don
这里是一个打印行号的程序:
Here is a program that prints line numbers:
file =``results.log'';
打开(INFO,
file = ''results.log'';
open(INFO,