Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mystery Character? 1

Status
Not open for further replies.

Geronimo24

Programmer
Dec 10, 2003
8
US
Hi,

I have a very simple Perl script that I am using to read in values from tab-delimited text files, reformat those values and print them to an output file.

I simply read in each line of the file and save each value from that line to a variable by using the split function. The one problem that I sometimes coming across is that sometimes when my program reads in a line,

Example:
Perl Java C

The last value that it reads has this very strange character appended to it. Using chop and chomp don't get rid of it! When I print out the corrupted value to my output file, it is as if it contains a carriage return at the end of that value (I get a new line after the value every time I print it out to my output file).

I put in some debug statements and got some very strange results ($value is the corrupted variable):

line 1 $howLong = length($value);
line 2 print "$value\n";
line 3 print "length: $howLong\n";
line 4 print "$value hello\n";
line 5 print "done\n";

Assuming that $value contains "dinosaur@hotmail.com," the ouput is:

dinosaur@hotmail.com
length: 19 [length should be 18!]
[space]hellour@hotmail.com
done

The last line of the output is very puzzling!!!

When I modify the debug statements so that I take out the \n from lines 2 and 4...

line 1 $howLong = length($value);
line 2 print "$value";
line 3 print "length: $howLong\n";
line 4 print "value hello";
line 5 print "done\n";

I get this even weirder outputoutput (assume $value contains dinosaur@hotmail.com:

length 19hotmail.com
[space]hellodoneotmail.com

The only way that I have been able to get rid of this puzzling character that somehow corrupts my variable is by doing this:

$value = substr($value, 0, length($value) - 2))

Is there a cleaner, safer way that I can get rid of this mystery character? I don't want to indiscriminately always chop off the last character since I don't always have the problem. Someone told me it might be a ^M character, but doing a substitution in my script does not get rid of it!

Anyone have any idea how to fix this??
 
Sounds like your source file was created in windows and you're running the script in a *nix environment. The ^M character mentioned is actually a "\r" in code (the first half of a windows newline), so you could just remove all of those from the input. Often times, you'll see [tt]s/\r\n/\n/g[/tt] to convert windows newlines to *nix newlines, but in this case, could could probably get away with [tt]s/\r//g[/tt].

Hope it helps,
Andrew

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
My boss is a mystery character, chomp and chop don't seem to work with him either. That's a different story though.
:))
 
Thanks a lots. /\r\n/\n/g fixed everything! That was a big help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top