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 6722085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:24:20+00:00 2026-05-26T09:24:20+00:00

I am using the following Perl script to do some simple processing: use strict;

  • 0

I am using the following Perl script to do some simple processing:

use strict;
my $file = "data-text";
open(FILE, "<$file") or die "Can't open $file: $!\n";
my @lines = <FILE>;
close FILE;
my @arrayA = (); my @arrayB=();
my $i = 0;
while($i < @lines) {
    print $lines[$i], "\t", $lines[$i+1], "\n";
    chomp($lines[$i]); chomp($lines[$i+1]); #The problem is here...
    push @arrayA, \$lines[$i];
    push @arrayB, \$lines[$i+1];
    print $lines[$i], "\t", $lines[$i+1], "\n";
    $i+=2;
}

As I indicated in the script, the problem is at the line chomp($lines[$i]); chomp($lines[$i+1]);. It seems if I use this line, the lines would be messed up.

What is wrong? Why is this?

  • 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-05-26T09:24:21+00:00Added an answer on May 26, 2026 at 9:24 am

    chomp deletes a single \n character from the end of a string.

    If the string ends with \r\n (the Windows-style line ending), chomp will leave the \r in place. This would likely result in symptoms similar to what you’re seeing.

    EDIT:

    Some background. Unix-like systems (including Linux) use a single line-feed character ('\n') to mark the end of each line in a text file. Windows (and its predecessor MS-DOS) uses two characters, a carriage return and a line feed (\r\n).

    Many of Perl’s features are designed to work with text. Which means, quite reasonably, that Perl assumes by default that any text file it’s reading uses the native end-of-line representation of the underlying operating system.

    A feature Perl inherited from C is that, when reading a line of text, the native end-of-line sequence, whatever it is, is translated to a single '\n' character. (The reverse translation is done on output). This frees most programs from having to worry about how text is represented; it’s translated to and from a canonical internal form on input and output. (That form happens to match the Unix format, for historical reasons.)

    But that doesn’t help much if you need to deal with non-native text files. If you’re running in a Unix-like environment, but reading Windows-format text files, the \r characters are going to look like part of the line. In particular, chomp won’t do anything special with them. And when you print a \r character, it typically causes the cursor to move to the beginning of the current line without advancing to the next line. It’s a mess. (Cygwin is a rich source of such confusion; it’s a Unix-like environment, using Unix-style text files by default, but it runs under Windows with full visibility to the Windows file system. Are you using Cygwin?)

    See @BillRupert’s comment; he’s running under Windows with a Windows native implementation of Perl, so he doesn’t see the problem you’re having.

    If you want to deal with non-native text files, you’ll need to do a little extra work. For example, when reading a line of text, rather than just

    chomp $line;
    

    you might write:

    chomp $line;
    $line =~ s/\r$//;
    

    And when writing text, you can do this:

    $line =~ s/$/\r/;
    

    But first you’ll need to decide whether you want to write the output with Windows-style or Unix-style line endings. It’s tricky.

    (There’s probably a Perl module that makes this easier; anyone who knows of one, please mention it in a comment.)

    Incidentally, the output you’re seeing isn’t the output your program is producing. If you filter your output through something that shows non-printable characters in printable form, you’ll see \r or ^M in your output. Use ... | cat -A or ... | cat -v if your system has the cat command.

    If possible, you might consider translating your input before trying to read it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a simple Perl script to monitor a log file using the
I can open a Terminal tab using the following AppleScript: tell application Terminal set
I have a config file that contains the following line: pre_args='$REGION','xyz',3 Using perl from
I am writing a Perl script that needs to extract some data from an
I have a Perl script that I've written and can compile it using pp
I'm trying to clean up form input using the following Perl transliteration: sub ValidateInput
I am using following snippet to build a 32 bit integer to use with
I have a perl script that will perform some operations on directories, and I
I'm using Thread::Pool::Simple to create a few working threads. Each working thread does some
I am trying to run simple perl dbi example script to connect to mysql

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.