简体   繁体   English

Perl的Chomp:Chomp将删除整个单词而不是换行符

[英]Perl's Chomp: Chomp is removing the whole word instead of the newline

I am facing issues with perl chomp function. 我遇到了perl chomp函数的问题。 I have a test.csv as below: 我有一个test.csv ,如下所示:

col1,col2
vm1,fd1
vm2,fd2
vm3,fd3
vm4,fd4

I want to print the 2nd field of this csv. 我想打印此csv的第二个字段。 This is my code: 这是我的代码:

#!/usr/bin/perl -w
use strict;

my $file = "test.csv";
open (my $FH, '<', $file);
my @array = (<$FH>);
close $FH;

foreach (@array)
{
    my @row = split (/,/,$_);
    my $var = chomp ($row[1]);     ###   <<< this is the problem
    print $var;
}

The output of aboe code is : aboe代码的输出为:

11111

I really don't know where the "1" is comming from. 我真的不知道“ 1”从何而来。 Actually, the last filed can be printed as below: 实际上,最后提交的文件可以打印如下:

foreach (@array)
{
    my @row = split (/,/,$_);
    print $row[1];     ###  << Note that I am not printing "\n"
}  

the output is: 输出为:

vm_cluster
fd1
fd2
fd3
fd4

Now, i am using these field values as an input to the DB and the DB INSERT statement is failing due this invisible newline. 现在,我将这些字段值用作数据库的输入,并且由于此不可见的换行符,数据库INSERT语句失败。 So I thought chomp would help me here. 所以我认为排骨可以在这里帮到我。 instead of chomping, it gives me "11111". 而不是砍,它给我“ 11111”。

Could you help me understand what am i doing wrong here. 你能帮我了解我在这里做错了吗。

Thanks. 谢谢。

Adding more information after reading loldop's responce: 阅读loldop的回复后添加更多信息:

If I write as below, then it will not print anything (not even the "11111" output mentioned above) 如果我这样写,那么它将不会打印任何内容(甚至上面提到的“ 11111”输出也不会)

foreach (@array)
{
    my @row = split (/,/,$_);
    chomp ($row[1]); 
    my $var = $row[1]; 
    print $var;
}

Meaning, chomp is removing the last string and the trailing new line. 意思是,chomp删除了最后一个字符串和尾随的新行。

The reason you see only a string of 1 s is that you are printing the value of $val which is the value returned from chomp . 您仅看到1 s的字符串的原因是,您正在打印$val的值,该值是chomp返回的值。 chomp doesn't return the trimmed string, it modifies its parameter in-place and returns the number of characters removed from the end. chomp不返回修剪后的字符串,而是就地修改其参数并返回从末尾删除的字符数。 Since it always removes exactly one "\\n" character you get a 1 output for each element of the array. 因为它总是删除精确的一个"\\n"字符,所以您为数组的每个元素都得到1输出。

You really should use warnings instead of the -w command-line option, and there is no reason here to read the entire file into an array. 您确实应该use warnings而不是-w命令行选项,并且这里没有理由将整个文件读入数组。 But well done on using a lexical filehandle with the three-parameter form of open . 但是使用带有三参数形式open的词汇文件句柄做得很好。

Here is a quick refactoring of your program that will do what you want. 这是程序的快速重构,可以完成您想要的操作。

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

my $file = 'test.csv';
open my $FH, '<', $file or die qq(Unable to open "$file": $!);

while (<$FH>) {
    chomp;
    my @row = split /,/;
    print $row[1], "\n";
}

although, it is my fault at the beginning. 不过,一开始是我的错。
chomp function return 1 <- result of usage this function. chomp函数return 1 <-使用此函数的结果。
also, you can find this bad example below. 另外,您可以在下面找到这个不好的例子。 but it will works, if you use numbers. 但是,如果您使用数字,它会起作用。 sometimes i use this cheat (don't do that! it is my bad-hack code!) 有时我会使用这种作弊手段(不要那样做!这是我的恶意代码!)
map{/filter/ && $_;}@all_to_filter;
instead of this, use 代替这个,使用
grep{/filter/}@all_to_filter;

foreach (@array)
{
    my @row = split (/,/,$_);
    my $var = chomp ($row[1]) * $row[1];     ###   this is bad code!
    print $var;
}

foreach (@array)
{
    my @row = split (/,/,$_);
    chomp ($row[1]);
    my $var = $row[1];  
    print $var;
}

If you simply want to get rid of new lines you can use a regex: 如果您只是想摆脱换行,可以使用正则表达式:

my $var = $row[1];
$var=~s/\n//g;

So, I was quite frustrated with this easy looking task bugging me for the whole day long. 因此,这一整日困扰我的任务看上去很容易让我感到沮丧。 I really appreciate everyone who responded. 我非常感谢每个做出回应的人。

Finaly I ended up using Text::CSV perl module and then calling each of the CSV field as array reference. 最后,我最终使用了Text :: CSV perl模块,然后调用了每个CSV字段作为数组引用。 There was no need left to run the chomp after using Text::CSV. 使用Text :: CSV之后,无需运行chomp。

Here is the code: 这是代码:

#!/usr/bin/perl 
use warnings;
use strict;
use Text::CSV;


 my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                  or die "Cannot use CSV: ".Text::CSV->error_diag ();


open my $fh, "<:encoding(utf8)", "vm.csv" or die "vm.csv: $!";

<$fh>;  ## this is to remove the column headers.

while ( my $row = $csv->getline ($fh) )
{
    print $row->[1];
}

and here is hte output: 这是hte输出:

fd1fd2fd3fd4 

Later i was pulled these individual values and inserted into the DB. 后来我被拉出这些单独的值并插入到数据库中。

Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM