Link to home
Start Free TrialLog in
Avatar of djc2
djc2

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of n0thing
n0thing
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
You could also define
  $/= "\cM";
then your
  while( <INPUT_FILE> ){ chomp; @values=split; ... }
will treat ^M as the end of line

Avatar of djc2
djc2

ASKER

$/= "\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!