Click to See Complete Forum and Search --> : Perl - strange control character appearing


Malone
09-15-2004, 11:06 PM
I have a list of email addresses in one file, each on its own line.

I'm trying to output them to another file as a list separated by commas. Here is the code in question.

while (chomp($line = <MEMBER_LIST>))
{
print D_LIST $line . ',';
}


It gives me the email address separated by commas like I want, but it also throws in a "^M" after each address.

Like:
addr1@blah.edu^M,addr2@blah.edu^M,addr3@blah.edu^M,...

When I change that comma in the code to a newline character, the "^M" disappears. Any idea on why it's doing this?

Malone
09-16-2004, 01:46 AM
Nevermind, good old dos2unix fixed it.

Grizzly
09-16-2004, 01:21 PM
Yep, what you were seeing was a pirce of DOS/Windows newline characters. The character you were seeing represented as "^M" was actually the carriage return character of the ascii character set (decimal value of 13; often short-handed as \r)

Windows uses 2 characters to represent a newline (carriage return + line feed), while Unix only uses one character (line feed)

rock
09-16-2004, 01:50 PM
Yep, and Macs (before they become Unix with OS X) just used the other one:

Windows: \r\n
Unix, Linux: \n
Mac: \r

One of the many annoyances of cross-platform work.

Grizzly
09-17-2004, 09:57 AM
Oh wow really? I didn't know that about Macs...that's pretty funny. I never really respected them until OS X anyways :)

Good to know though, thanks for the info rock - you learn something new every day.

gameboy1234
09-18-2004, 07:55 PM
The Macs did that because the old Apple II line did the same thing, and they just picked it up as an Apple tradition or whatever. I don't think there was much standardization of text file formats back then. Anyone know what CP/M used?

If you transfer a file with FTP, and set the transfer type to ASCII, it will correct the end of line silliness, if both systems identify themselves correctly. Of course, if you transfer a file with FTP in ASCII mode that is not text, you'll really hose it up.