in reply to Perl to C problem
Hello lbrandewie, and welcome to the Monastery!
Have a look at perlport#Newlines. \n is Perl’s logical newline, so when reading from a file on the Windows platform, the 2-character sequence \x0D\x0A (represented here in hexadecimal) is translated to \n; and when writing to a file, \n is translated to \x0D\x0A.
If you want to distinguish between \x0A (Unix) and \x0D\x0A (Windows) in your input files, then you can read using the :raw layer:
use strict; use warnings; use autodie; my $file = '1977_SoPW.txt'; open(my $fh, '<:raw', $file); my @test = <$fh>; close $fh; for my $line (@test) { print ord(substr($line, -2, 1)), "\t"; print ord(substr($line, -1, 1)), "\n"; } print "\n";
Output (where the input file “1977_SoPW.txt” contains 3 sentences, the first and third terminated by \x0D\x0A, the second by just \x0A):
16:51 >perl 1977_SoPW.pl 13 10 46 10 13 10 16:51 >
(46 is the decimal code for ASCII character ., the full stop or period, which happened to be the last text character in the line.)
On the other hand, if you want to distinguish between different newline representations in your code, then you should either use literal rather than logical characters:
my $test = "\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A";
or add \r as haukex says above:
my $test = "\r\n\r\n\r\n\r\n\r\n";
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl to C problem
by lbrandewie (Acolyte) on Feb 16, 2019 at 18:10 UTC |