http://www.perlmonks.org?node_id=983589

slayedbylucifer has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am facing issues with perl chomp function. I have a test.csv as below:

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

I want to print the 2nd field of this 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 :

11111

I really don't know what is "11111". Actually, the last field 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. So I thought chomp would help me here. instead of chomping, it gives me "11111".

I also tried below loop:

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

But above loop will not print anything (not even the "11111" output mentioned above). Meaning, chomp is removing the last string and the trailing new line.

Could you help me understand what am i doing wrong here.

Also please find my discussion on stackoverflow for this issue: http://stackoverflow.com/questions/11645696/perls-chomp-chomp-is-removing-the-whole-word-instead-of-the-newline

Thanks.

Replies are listed 'Best First'.
Re: Chomp is removing the whole word instead of the newline
by Ratazong (Monsignor) on Jul 25, 2012 at 09:34 UTC

    From the docs:

    It (chomp) returns the total number of characters removed from all its arguments

    So   my $var = chomp ($row[1]); is not doing what you expect it to do.

    Rata
      You know what, i did read exact same line in the documentation but I think i did not interpret it correctly. thanks for your time.
Re: Chomp is removing the whole word instead of the newline
by tobyink (Canon) on Jul 25, 2012 at 09:35 UTC

    chomp operates in-place. The return value isn't especially useful.

    use 5.010; say "enter some text lines"; while (my $line = <>) { say "\$line is [[$line]]"; say "doing the chomp thing!"; my $return = chomp $line; say "\$line is now [[$line]]"; say "\$return is [[$return]]"; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Correct, As Ratazong said above, I could not interpret this information correctly. Thnaks for your time.
Re: Chomp is removing the whole word instead of the newline
by choroba (Cardinal) on Jul 25, 2012 at 09:38 UTC
    Your last example works for me, that's how chomp should be used. The output:
    col2fd1fd2fd3fd4
    Notice there is no newline at the end. The output might be buffered, i.e. it will not be shown before you print a newline or finish the script.
      exactly. On my fedora, It does show output. But on my ubuntu, it does not show any output unless I add "\n" to the print statement. Is this an expected behaviour ?
Re: Chomp is removing the whole word instead of the newline
by thonar (Monk) on Jul 25, 2012 at 13:03 UTC
    ... my @row = split (/,/,$_); # I believe this is what you trying to do chomp( my $var = $row[1]); # Or maybe a little bit more understandable $var = $row[1]; chomp $var; # Return the total number of characters removed in $row[1] to $chomp_r +esult # (in your case every 1 stands for a character chomp found in $row[1] +could # be a line break or something like that ) my $chomp_result = chomp( $row[1]); print "$var || $chomp_result\n"; ...
    I hope you get what i'm trying to say ...

      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. There was no need left to run the chomp after using Text::CSV.

      Here is the code:

      #!/usr/bin/perl use warnings; use strict; use Text::CSV; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attr +ibute. 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:

      fd1fd2fd3fd4

      Later i was pulled these individual values and inserted into the DB.

      Thanks everyone.

      Agree. Thanks for your time.
Re: Chomp is removing the whole word instead of the newline
by Anonymous Monk on Jul 25, 2012 at 09:41 UTC

    I am facing issues with perl chomp function ... Could you help me understand what am i doing wrong here.

    Well, for some reason, you're not copying the example from the chomp documentation