Perl Concat 字符串截断行首

发布于 2024-12-27 08:01:33 字数 837 浏览 3 评论 0原文

我在 perl 中遇到了一个奇怪的问题,我似乎无法找到答案。

我有一个小脚本,可以解析来自外部来源的数据(无论是文件、网站等)。解析数据后,它会将其保存到 CSV 文件中。但是,问题是当我写入文件或打印以筛选数据时,它似乎会截断字符串的开头。我正在使用严格和警告,但没有看到任何错误。

这是一个示例:

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

它将返回以下内容:

John
Blue
7.99
5

如果我尝试这样做:

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

我得到以下结果:

,7.99,5

我尝试了以下操作来查看问题从哪里开始并得到以下结果:

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

结果:

John
John,Blue
,7.99
,7.99,5

我仍在学习 perl ,但似乎无法找出(可能是由于缺乏知识)造成这种情况的原因。我尝试调试脚本,但我没有在价格变量中看到任何会导致此问题的特殊字符。

I am running into a strange issue in perl that I can't seem to find an answer for.

I have a small script that will parse data from an external sorce (be it file, website, etc). Once the data has been parsed, it will then save it to a CSV file. However, the issue is when I am writing the file or printing to screen the data, it seems to be truncating the beginning of the string. I am using strict and warnings and I am not seeing any errors.

Here is an example:

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

It will return the following:

John
Blue
7.99
5

If I attempt to do it this way:

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

I get the following as a result:

,7.99,5

I tried the following to see where the issue begins and get the following:

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

Results:

John
John,Blue
,7.99
,7.99,5

I am still learning perl, but can't seem to find out (maybe due to lack of knowledge) of what is causing this. I tried debugging the script, but I did not see any special character in the price variable that would cause this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2025-01-03 08:01:33

$price 中的字符串以回车符结尾。这会导致您的终端将光标移动到行首,导致前两个字段被后面的字段覆盖。

您可能正在 unix 机器上阅读 Windows 文本文件。转换文件(例如,使用 dos2unix),或使用 s/\s+\z//; 而不是 chomp;

如果 CR 位于字符串的中间,则可以使用 s/\r//g;。

The string in $price ends with a Carriage Return. This is causing your terminal to move the cursor to the start of the line, causing the first two fields to be overwritten by the ones that follow.

You are probably reading a Windows text file on a unix box. Convert the file (using dos2unix, for example), or use s/\s+\z//; instead of chomp;.

If the CR made into the middle of a string, you could use s/\r//g;.

水染的天色ゝ 2025-01-03 08:01:33

根据@Mat的建议,我通过hexdump -C运行输出,发现有一个回车符(由十六进制值0d表示)。使用代码 $price =~ s/\r//g; 从文本行中删除 CR 解决了该问题。

另外,输入文件是 Windows 格式而不是 Unix,运行命令 dos2unix 来修复该问题。

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/\r//g; to remove the CR from the line of text fixed the problem.

Also, the input file was in Windows format not Unix, ran the command dos2unix to fix that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文