排骨澄清 [英] Clarification on chomp

查看:125
本文介绍了排骨澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在放假,决定花时间学习Perl.我正在使用Beginning Perl( http://www.perl.org/books/beginning- perl/),我将在第三章结尾处完成练习.

I'm on break from classes right now and decided to spend my time learning Perl. I'm working with Beginning Perl (http://www.perl.org/books/beginning-perl/) and I'm finishing up the exercises at the end of chapter three.

其中一项练习要求我将您的重要电话号码存储在哈希中.编写一个程序,以该人的姓名查找号码."

One of the exercises asked that I "Store your important phone numbers in a hash. Write a program to look up numbers by the person's name."

无论如何,我想出了这个:

Anyway, I had come up with this:

#!/usr/bin/perl
use warnings;
use strict;

my %name_number=
(
Me => "XXX XXX XXXX",
Home => "YYY YYY YYYY",
Emergency => "ZZZ ZZZ ZZZZ",
Lookup => "411"
);

print "Enter the name of who you want to call (Me, Home, Emergency, Lookup)", "\n";
my $input = <STDIN>;
print "$input can be reached at $name_number{$input}\n";

那是行不通的.我不断收到此错误消息:

And it just wouldn't work. I kept getting this error message:

在连接(.)或hello.plx中的字符串中使用未初始化的值 第17行,第1行

Use of uninitialized value in concatenation (.) or string at hello.plx line 17, line 1

我尝试了一些更多的代码,但是每个解决方案"看起来都比之前的解决方案"复杂.最后,我决定检查答案.

I tried playing around with the code some more but each "solution" looked more complex than the "solution" that came before it. Finally, I decided to check the answers.

我的代码和答案之间的唯一区别是在<STDIN>;之后出现了chomp ($input);.

The only difference between my code and the answer was the presence of chomp ($input); after <STDIN>;.

现在,作者在先前的示例中使用了chomp,但是他并没有真正涵盖chomp的工作.因此,我在www.perlmeme.org上找到了这个答案:

Now, the author has used chomp in previous example but he didn't really cover what chomp was doing. So, I found this answer on www.perlmeme.org:

chomp()函数将(通常)从中删除任何换行符 字符串的结尾.我们通常说的原因是它实际上 删除与当前$/值匹配的任何字符(输入 记录分隔符),$/默认为换行符.

The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline..


无论如何,我的问题是:


Anyway, my questions are:

  1. 正在删除哪些换行符? Perl是否会自动将"\n"附加到<STDIN>的输入中?我只是有点不清楚,因为当我读到它实际上删除了与$/的当前值匹配的任何字符"时,我不禁想到我不记得在代码中的任何地方放置$/了" ."

  1. What newlines are getting removed? Does Perl automatically append a "\n" to the input from <STDIN>? I'm just a little unclear because when I read "it actually removes any character that matches the current value of $/", I can't help but think "I don't remember putting a $/ anywhere in my code."

我想尽快制定最佳实践-是始终在<STDIN>之后始终包含chomp还是在不必要的情况下?

I'd like to develop best practices as soon as possible - is it best to always include chomp after <STDIN> or are there scenarios where it's unnecessary?

推荐答案

<STDIN>读取到输入字符串的末尾,如果按回车键,则包含换行符.您可能会这样做.

<STDIN> reads to the end of the input string, which contains a newline if you press return to enter it, which you probably do.

chomp删除字符串末尾的换行符. $/是您可能不必担心的变量(如您所见,默认为换行符).它只是告诉perl输入记录分隔符"是什么,我假设这意味着它定义了<FILEHANDLE>读取的距离.您现在几乎可以忘记它,这似乎是一个高级话题.只是假装chomp排在结尾的换行符即可.老实说,我以前从未听说过$/.

chomp removes the newline at the end of a string. $/ is a variable (as you found, defaulting to newline) that you probably don't have to worry about; it just tells perl what the 'input record separator' is, which I'm assuming means it defines how far <FILEHANDLE> reads. You can pretty much forget about it for now, it seems like an advanced topic. Just pretend chomp chomps off a trailing newline. Honestly, I've never even heard of $/ before.

对于您的其他问题,通常不选变量,以后再根据需要添加换行符通常是更清洁的方法,因为您并不总是知道变量是否具有换行符.通过总是剔除变量,您总是会得到相同的行为.在某些情况下,它是不必要的,但是如果您不确定chomp它不会对您造成伤害.

As for your other question, it is generally cleaner to always chomp variables and add newlines as needed later, because you don't always know if a variable has a newline or not; by always chomping variables you always get the same behavior. There are scenarios where it is unnecessary, but if you're not sure it can't hurt to chomp it.

希望这会有所帮助!

这篇关于排骨澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆