我的代码如下:
#!/usr/bin/perl
use strict;
use warnings;
open(IO,"<source.html");
my $variable = do {local $/; <IO>};
chomp($variable);
print $variable;
Run Code Online (Sandbox Code Playgroud)
但是,当我打印它时,它仍然有换行符?
我试图使用正则表达式获取Perl中两个分隔符之间的值.我正在打开一个文件并使用chomp逐行浏览文件.该文件的外观示例:
"This is <tag> an </tag> example
of the <tag> file </tag> that I
am <tag> trying </tag> to <tag> parse </tag>"
Run Code Online (Sandbox Code Playgroud)
我能够得到前几个词:"an","file",但在第三行我只能"尝试"而不是"解析".这是我尝试使用的代码:
while (chomp($line = <$filename>)){
($tag) = $line =~ m/<tag>(.*?)<\/tag>/;
push(@tagarray, $tag);
}
Run Code Online (Sandbox Code Playgroud)
我怀疑这与某些事情有关,chomp但是没有看到如何以不同方式解析文件.
我有一个XML文件.文件中有一些空白行.如何只删除空白行?我尝试了chomp()方法,发现它将删除所有新的行符号.我仍然需要输出标准的XML文件格式.
while(my $line = <$fh>) {
#chomp($line);
#$line =~ s/\s+//g;
print $line;
}
__DATA__
<item>
<key>AB</key>
<info>
<format>10</format>
<description>Any binary string</description>
<value>NA</value>
<whereUsed>A2B25,A2B26</whereUsed>
</info>
</item>
Run Code Online (Sandbox Code Playgroud)
预期的输出形式.
<item>
<key>AB</key>
<info>
<format>10</format>
<description>Any binary string</description>
<value>NA</value>
<whereUsed>A2B25,A2B26</whereUsed>
</info>
</item>
Run Code Online (Sandbox Code Playgroud) 我正在尝试阅读此文件:
Oranges
Apples
Bananas
Mangos
Run Code Online (Sandbox Code Playgroud)
使用这个:
open (FL, "fruits");
@fruits
while(<FL>){
chomp($_);
push(@fruits,$_);
}
print @fruits;
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何输出.我在这里错过了什么?我正在尝试将文件中的所有行存储到一个数组中,并在一行中打印出所有内容.为什么不选择从文件中删除换行符,就像它应该的那样?
如果一个特定的字符串包含一个不可见的换行符(不是\n,而是十六进制的0A,因为这个值是从数据库传递下来的),我怎样才能把它去掉呢?Apache Chomp 有帮助吗?
从数据库返回的文本的十六进制形式为“5761 6920 4D61 6E0D 0A”
它翻译为“Wai Man”,带有回车符。
chomp功能的目的和优点是什么?它能做什么?使用chomp会产生任何问题吗?或者在文件打开后使用chomp是必要的吗?
我正在尝试使用chomp()从文件中删除所有换行符.这是代码:
use strict;
use warnings;
open (INPUT, 'input.txt') or die "Couldn't open file, $!";
my @emails = <INPUT>;
close INPUT;
chomp(@emails);
my $test;
foreach(@emails)
{
$test = $test.$_;
}
print $test;
Run Code Online (Sandbox Code Playgroud)
并且input.txt文件的测试内容很简单:
hello.com
hello2.com
hello3.com
hello4.com
Run Code Online (Sandbox Code Playgroud)
我的预期输出是这样的:hello.comhello2.comhello3.comhello4.com
但是,我仍然得到与输入文件相同的内容,请帮忙吗?
谢谢
puts "Enter the first number"
num1 = Float(gets)
puts "Enter the second number"
num2 = Float(gets)
puts "Enter the operation"
op = gets
op = op.chomp # <--- THIS LINE!
case op
when "+" then puts num1 + num2
when "-" then puts num1 - num2
when "*" then puts num1 * num2
when "/" then puts num1 / num2
end
Run Code Online (Sandbox Code Playgroud) 我有一个字符串 str a\tb\tc\td\te
我希望第一个字段值a进入变量,然后第二个字段值b进入其他变量,然后c\td进入第三个变量和最后一个字段值e进入一个变量.
如果我做
my ($a,$b,$c,$d) = split(/\t/,$_,4);
Run Code Online (Sandbox Code Playgroud)
$c只会收购c和$d将收购d\te
我可以:
my ($a,$b,$c) = split(/\t/,$_,3);
Run Code Online (Sandbox Code Playgroud)
然后c会得到c\td\te
,我可以以某种方式(如何?)摆脱最后的价值并把它拿进去$d
怎么做到这一点?
在我的代码中,我将一个变量光盘分配给disc我的linux系统上的命令结果.这输出字符串RESEARCH
my $disc = `disc`;
print "$disc\n";
$disc = chomp($disc);
print "$disc\n";
Run Code Online (Sandbox Code Playgroud)
但是,当我使用chomp从字符串中去除换行符时,它将字符串更改为1.这是输出
RESEARCH
1
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?