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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with reading in lines that contain carriage returns 1

Status
Not open for further replies.

Timtico

Technical User
Dec 20, 2008
8
0
0
NL
Hi, I'm a scientist using PERL to write some very handy scripts. I spent alot of hours writing my first script, and it works very well! however I'm running into some problems:


This is the type of info that I use as input, it's contained in a plain text file (txt):

An15g05370
An09g00720
An04g05050
An16g03160
An08g11930
...

This is part of the program I use to populate an array line by line based on the above textfile:

print "\nPlease type the name of the file with geneIDs and press ENTER: ";
$lookupfile_name = <STDIN>;
chomp $lookupfile_name;

#Opening the lookupfile containing the GeneID's and
open (LOOKUPFILE, "$lookupfile_name") or (die "\n\nSomething terrible has gone wrong! The file you specified could not be opened\n\n");

while ($input = <LOOKUPFILE>)
{
$input =~ s/\r//g;
push (@lookupgenes, $input);
}

I tried a number of different substitutions to get rid of the \r (eg. s/\r/\n//g) but nothing seems to work.

For some reason PERL doesnt replace the \r in the input file. However, when I manually replace \r with \n using the advanced search and replace function in my text editor and use that modified file, my script works perfectly fine!
however, I'm unable to remove all \r using PERL.

 
Did chomp not work on the input from your text file?
 
I was thinking that chomp only removes \n, anyway the following code is not working either to remove carriage returns.

the following code is not working either

while ($input = <LOOKUPFILE>)
{
chomp $input;
$input =~ s/\r//g;
push (@lookupgenes, $input);
}

............

while ($input = <LOOKUPFILE>)
{
$input =~ s/\r//g;
chomp $input;
push (@lookupgenes, $input);
}

I think it might be a problem with the textfile which makes some kind of weird \r\n line end to it.. would explain while manual deleting the \r from the file does give good results.
 
Windows uses \r\n but chomp on Windows removes \r\n. The problem can arises when you upload a Windows file to a Linux/Unix box using binary instead of text transfer mode or are using an FTP application that does not know how to change the line endings accordingly during transfer.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm using a Mac.

I know my file contains \r as newlines, the texteditor I use can replace it.. when I replace \r with \n, I can use the file without any problems with my PERL script. But I want my script to be more flexible and be able to handle textfiles from multiple sources without the need to manually change \r to \n (most of them wouldnt know how to). The script needs to be flexible since most of the users will have different operating systems...

any more suggestions Kevin?
 
This might help:
Code:
while (my $input = <LOOKUPFILE>) {
	$input =~ s/[\x0A\x0D]+/\n/g;
	my @temp = split("\n", $input);
	push (@lookupgenes, @temp);
}
 
omg! Thanx alot Harsh, that works perfectly!

Whats x0A and x0D?
 
The ASCII decimal codes for Line Feed (10) and Carriage Return (13) in hex are 0A and 0D respectively.

So that regex will take any combination of Line Feed and Carriage Return characters and substitute them with the native new line character(s).
 
Ok, thanx for the clarification!

My little script runs very sweet now, thanx!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top