Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Opening text files with CR format

by knith (Initiate)
on Aug 03, 2009 at 12:07 UTC ( #785421=perlquestion: print w/replies, xml ) Need Help??
knith has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to open some text files saved with the format CR (MAC) using perl, I am having the problem that the open function does'nt recongnize the newline character. The solution might be to open every file and saved them with a format CR+LF (DOS) but I have to open more than a hundred files in different folders and I don't want to go one by one. Please Monks I need some help here. This is the code I am using I am only including the opening part ;)
use strict; use warnings; @files = <*.tx>; print "@files \n"; foreach $file (@files) { print " \n Working with file: $file \n"; my @lines=(); open(INFO, $file)or die "Could not open file: $!" ; @lines=<INFO>; close(INFO); ... }

Replies are listed 'Best First'.
Re: Opening text files with CR format
by linuxer (Curate) on Aug 03, 2009 at 12:12 UTC


    have a look at perlvar; search for $INPUT_RECORD_SEPARATOR

    You can set that variable to the needed character/string to parse your files...

    Example snippet:

    #! /usr/bin/perl use strict; use warnings; # ... for my $file ( @files ) { if ( open my $fh, '<', $file ) { # see perldoc perlvar local $/ = "\x0d"; @lines = <$fh>; } else { warn "$file: $!\n"; } }
      Thanks a million, you just saved me a lot of frustation time :P
Re: Opening text files with CR format
by moritz (Cardinal) on Aug 03, 2009 at 12:13 UTC
    You can set $/ to "\r", then perl will read lines separated by CR instead.

    See also: perlvar, perlport

Re: Opening text files with CR format
by Bloodnok (Vicar) on Aug 03, 2009 at 12:30 UTC
    Unless I've misunderstood the question, I think that the solution offered by both moritz & linuxer have missed the point - AFAICT, the OP appears to be experiencing problems actually opening files c/w reading a successfully opened file.

    Never having used a Mac (thus not being aware of the pitfalls), my only suggestion would be to change:

    @files = <*.tx>;
    to read:
    @files = <*.tx>; chomp @files;
    thus removing any trailing whitespace from the filename(s).

    A user level that continues to overstate my experience :-))
      Hi, sorry maybe I was not so clear with the question, I am using a normal computer with windows, the files that I am working with where stored by someone using a mac, that is why new line indicator is different and not recognized by the open function in perl. It took me a while to realized why it was not working properly. First I started programming with perl since a couple of hours and the second thing is that my text editor can handle mac, unix, dos terminations, giving no hint what kind of format for the new line was used. Thanks for the answer anyway :))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://785421]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-01-23 06:57 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (191 votes). Check out past polls.