Question

I want to read an input file line by line, but this file has unknown ending character.

Editor vim does not know it either, it represents this character as ^A and immediately starts with characters from new line. The same is for perl. It tried to load all lines in once, because it ignores these strange end of line character.

How can I set this character as end of line for perl? I don't want to use any special module for it (because of our strict system), I just want to define the character (maybe in hex code) of end of line.

The another option is to convert the file to another one, with good end of line character (replace them). Can I make it in some easy way (something like sed on input file)? But everything need to be done in perl.

It is possible?

Now, my reading part looks like:

open (IN, $in_file);
$event=<IN>;   # read one line
Was it helpful?

Solution

The ^A character you mention is the "start of heading" character. You can set the special Perl variable $/ to this character. Although, if you want your code to be readable and editable by the guy who comes after you (and uses another editor), I would do something like this:

use English;

local $INPUT_RECORD_SEPARATOR = "\cA" # 'start of heading' character

while (<>)
{
    chomp; # remove the unwanted 'start of heading' character
    print $_ . "\n";
}

From Perldoc:

$INPUT_RECORD_SEPARATOR
$/

The input record separator, newline by default. This influences Perl's idea of what a "line" is.

More on special character escaping on PerlMonks.

Oh and if you want, you can enter the "start of heading" character in VI, in insert mode, by pressing CTRL+V, then CTRL+A.

edit: added local per Drt's suggestion

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top