Improve company productivity with a Business Account.Sign Up

x
?
Solved

line input doesn't recognize ^M end of line character

Posted on 1998-07-02
3
Medium Priority
?
157 Views
Last Modified: 2010-03-05
I'm working on a Linux computer with perl-5.004-1 and trying to input an ascii data file that was created on a Macintosh computer. The file is
huge and is composed of columns of 3 rows each. The problem is
that the Macintosh newline character (^M) does not seem to be recognized
by Perl as such.  So, when I try to do something like:

         while ($line=<INPUT_FILE>) {
            chop $line;
            @values=split(/ /,$line); #store columns in values array
            print SEGMENTED_FILE "$values[1]\n";
          }

in which I'm attempting to read in the file line by line and output column
1 to SEGMENTED_FILE, it doesn't work (It attempts to read in the entire
file as one line). If it was a Unix data file with a proper newline
character it works fine.

        So, my question is how do I get around this. If I was writing it
in C, then I know that I could read in one field at a time, and just deal
with it that way. In Perl can I do that? Or is there a way to get Perl to
recognize the ^M character as end of line? Any suggestions?

Thanks in advance,
Dave
0
Comment
Question by:djc2
3 Comments
 
LVL 5

Accepted Solution

by:
n0thing earned 600 total points
ID: 1208277
Just remove the ^M, by adding one liner "s/^V^M//;" after the while.
PS: You'll need to press "Ctrl-V, then Ctrl-M" in order for the
^M to come up.

Regards,
Minh Lai
0
 
LVL 85

Expert Comment

by:ozo
ID: 1208278
You could also define
  $/= "\cM";
then your
  while( <INPUT_FILE> ){ chomp; @values=split; ... }
will treat ^M as the end of line

0
 

Author Comment

by:djc2
ID: 1208279
$/= "\cM" worked perfectly!

I didn't try Minh Lai's solution - since my data file is huge, I didn't want to read it all into memory and then process it after the while.

Thanks!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question