English 中文(简体)
管道开端
原标题:Perl Concat String Truncates Beginning of Line

我陷入一个奇怪的问题,我似乎无法找到答案。

我有少量文字,将数据从外部来源(如档案、网站等)。 这些数据一经分类,就会将其保存到CSV档案中。 然而,在我撰写文件或印刷文件以筛选数据时,这个问题似乎正在缩小范围。 我使用严格的警告,我没有看到任何错误。

例如:

print "Name: " . $name . "
";
print "Type: " . $type. "
";
print "Price: " . $price . "
";
print "Count: " . $count . "
";

委员会将恢复以下工作:

John
Blue
7.99
5

如果我试图这样做的话:

print "$name,$type,$price,$count
";

我得出以下结果:

,7.99,5

我试图了解这一问题的开始地点,并得出以下结论:

print "$name
";
print "$name,$type
";
print "$name,$type,$price
";
print "$name,$type,$price,$count
";

成果:

John
John,Blue
,7.99
,7.99,5

我仍在学习,但似乎无法找到(可能是由于缺乏知识)造成这种情况的原因。 我试图伪造文字,但我看不出价格变动中造成这种情况的特殊性。

最佳回答

Per @Mat suggestion I ran the output through hexdump -C and found there was a carriage return (indicated by the hex value 0d). Using the code $price =~ s/ //g; to remove the CR from the line of text fixed the problem.

此外,投入文件使用的是Windows格式而不是“Unix”,其指令为dos2unix

问题回答

<代码>价格的载体以“运回”结束。 这使得您的终点站能够把 cur子推向线的开端,使前两个领域被以下两个领域所取代。

你们或许可以在一个不定的箱子上读一个Windows文本文档。 更改文档(例如使用dos2unix),或使用s/s+z/;而不是chomp;

如将捷克共和国列入插图中,可使用<代码>/g;。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签