Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:32:25+00:00 2026-06-09T00:32:25+00:00

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

  • 0

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 where the “1” is comming from. 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. So I thought chomp would help me here. instead of chomping, it gives me “11111”.

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

Thanks.

Adding more information after reading loldop’s responce:

If I write as below, then it will not print anything (not even the “11111” output mentioned above)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:32:27+00:00Added an answer on June 9, 2026 at 12:32 am

    The reason you see only a string of 1s is that you are printing the value of $val which is the value returned from chomp. chomp doesn’t return the trimmed string, it modifies its parameter in-place and returns the number of characters removed from the end. Since it always removes exactly one "\n" character you get a 1 output for each element of the array.

    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. But well done on using a lexical filehandle with the three-parameter form of 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";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing some issues in loading a CSV file through SQL Loader. Here
I have been facing issue with sphinx result set for string attribute. Below is
i am facing issues while testing with the Test Accunt. i had entered correct
I have a page on iPad and am facing some issues implementing an equivalent
I am facing issues with two similar friendly urls. I have mentioned the following
Currently I am facing issues, that the java melody statistics running for a grails
I am facing some issues while serializing objects (I am using JBoss Drools, and
Well, since I am facing some issues with OAuth implementation, I decided to go
I'm working using scriptaculous library. However I'm facing some issues with inclusion of the
I got below JSON string, i facing issue parsing because it contains non english

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.