how to erase \r or \015 in win32?

N

news.hinet.net

i want to erase ^M in then end of lines.
but i can do this in win32.

$_=~s/\r//g; #but \r still exist

$e=\x0D;
$_=~s/$e//g; #have the same result

===================================
 
G

Gunnar Hjalmarsson

news.hinet.net said:
i want to erase ^M in then end of lines.
but i can do this in win32.

$_=~s/\r//g; #but \r still exist

Not for me.

$_ = "First line\r\nSecond line\r\n";
print length, "\n";
s/\r//g;
print length, "\n";

Printed result:
25
23
 
M

Marc Dashevsky

[email protected] says in article said:
i want to erase ^M in then end of lines.
but i can do this in win32.

$_=~s/\r//g; #but \r still exist

$e=\x0D;
$_=~s/$e//g; #have the same result

Maybe you don't have \r at the end of the lines, maybe
you really have the two characters "^M".
 
D

David Efflandt

i want to erase ^M in then end of lines.
but i can do this in win32.

$_=~s/\r//g; #but \r still exist

$e=\x0D;
$_=~s/$e//g; #have the same result

The last would fail because \x0D is a bareword and not a subroutine, so $e
probably has no value (should be quoted "\x0D").

If you only strip carriage returns and then handle or save it as text, the
remaining linefeed may automatically be converted back to carriage return,
linefeed pair. You may have to handle it in binmode to avoid that (or
remove them in an OS that does not use carriage returns for newlines).

In Windows "\n" eq "\015\012"
In Unix "\n" eq "\012"
In Mac "\n" eq "\015" (not sure about OSX)
 
A

Alan J. Flavell

If you only strip carriage returns and then handle or save it as text, the
remaining linefeed may automatically be converted back to carriage return,
linefeed pair.

If I may say so, that's a bit of a muddled presentation. There
shouldn't be any "may" about it: the behaviour in each situation is
clearly defined - one just has to know what it is.

I'd definitely recommend that the O.P read perldoc perlport with some
care, to get a fuller picture.

The issues are:

- which platform are we talking about?
- are we dealing with platform-native data, or could it be
platform-foreign?
- are we handling it in text mode or in binary mode?

(this last point gets more complex when we also have encoding
layers involved...)

If you're handling platform-native text data, and handling it in
text mode, then inside of your Perl program the newlines will be
"\n" (the associated \r being stripped off on input, and added on
output).
You may have to handle it in binmode to avoid that (or
remove them in an OS that does not use carriage returns for newlines).

In Windows "\n" eq "\015\012"

See above! In text mode I/O, "\n" is "\012" internally.
In Unix "\n" eq "\012"

Not if you're reading DOS-format data!

In fact the situation _could_ be just the opposite of what you said:
if the platform is Windows/DOS and you're working in text mode, then
with DOS-format data, the \015 will be removed on input; but if the
platform is Unix and the data is in DOS format, then on Unix you'll
get \015\012 _even_ when reading in text mode.
In Mac "\n" eq "\015"

OK. (...and then there's EBCDIC-based platforms ;-)

all the best
 
T

the zorg

Not for me.

$_ = "First line\r\nSecond line\r\n";

yes but have you not just typed a backslash and an 'r' and not the
control character "\r"?

print length, "\n";
s/\r//g;

You are just removing every character 'r' by replacing every occurence
of 'r' with "nothing".
 
J

Josef Möllers

the zorg wrote:

[ ] You have a real name
[X] You don't have a real name
yes but have you not just typed a backslash and an 'r' and not the
control character "\r"?

[ ] You know Perl
[X] You don't know Perl.
You are just removing every character 'r' by replacing every occurence
of 'r' with "nothing".

[ ] You know Perl
[X] You don't know Perl.

Try to read a book about Perl, then read it again and try to understand
what it says, then, if you succeed, get a real name and come back.
 
G

Gunnar Hjalmarsson

the said:
yes but have you not just typed a backslash and an 'r' and not the
control character "\r"?

No. The string is delimited with double-quotes.

We know very little about OP's actual problem, but I have a feeling
that David may have hit the nail on the head.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top