Chinaunix
标题:
chomp的问题!!
[打印本页]
作者:
zhou20071801
时间:
2012-05-25 21:13
标题:
chomp的问题!!
use strict;
use warnings;
open(RF,"<01.txt");
while(my $line=<RF>)
{
chomp($line);
print"$line";
}
为什么我加上chomp($line)后就只输出最后一个元素。去掉chomp($line)或者输出加上换行print"$line\n";输出就没有错??
跪求解释!!!chomp的问题困扰我很久了
作者:
zhou20071801
时间:
2012-05-25 21:19
另外:在windows下运行可以正确运行,但在linux下就不行。
作者:
ulovko
时间:
2012-05-25 21:42
回复
1#
zhou20071801
It works on a variable. The variable has to hold a string. And if the string ends in a newline
character, chomp can get rid of the newline. That’s (nearly) all it does.
chomp is actually a function. As a function, it has a return value, which is the number
of characters removed.
复制代码
作者:
RE_HASH
时间:
2012-05-26 06:30
大概你的文件是windows格式。
试试这个
s/[\r\n]$//;
作者:
yinyuemi
时间:
2012-05-26 15:00
回复
1#
zhou20071801
dos2unix 转下格式
作者:
sjdy521
时间:
2012-05-26 19:45
回复
1#
zhou20071801
use open IO => ":crlf"
作者:
JavasBoy
时间:
2012-05-28 22:42
#!/usr/bin/perl
use strict;
use warnings;
open(RF,"<01.txt");
while(my $line=<RF>)
{
chomp($line);
print"$line";
}
复制代码
我在Debian里用vim写入,运行正常。
作者:
zhou20071801
时间:
2012-05-30 22:57
谢谢!!这可行。但你能解释下出现这种状况的原因吗?
回复
6#
sjdy521
作者:
sjdy521
时间:
2012-05-31 11:02
回复
8#
zhou20071801
你把\r\n中的\n去掉,只剩\r了 当然显示上会出现问题了
作者:
zhou20071801
时间:
2012-06-02 19:07
谢谢!:wink:
回复
9#
sjdy521
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2