change file contents to a character array
Posted by yfpeng (yfpeng), 14 September 2002Hi guru:
I read in a text file into an array, but this array consists of every line (i.e. each line is an element of the array) in the file. Now I wish to convert this array into a character array - each character except newline character at the end of each line (use chop may cut this character) in the file is an element of the array. how to achieve this?thanks in advance.
Posted by John_Moylan (jfp), 14 September 2002I'm sure Graham will find holes in my code...but!
Use chomp() to get rid of new lines, it safer as it will only remove new lines, chop() will remove any trailing character.
I suppose here you could use tr/\n//; or s/\n//g; to get rid of newlines from the $string.
Hope this works, and will the board administrator point out flaws in my code please.
Posted by admin (Graham Ellis), 14 September 2002Nope, 'jpf', not going to find holes in your code. Should work just fine ....
You talk about chop and chomp removing end of line charcters - agreed, but not in your example code - because there you're reading the whole thing as a single scalar.
$string =~ s/[\n\r]//g;
will get rid of all the new line and carriage return characters for you, and will work no matter whether you data originated on a PC, a Mac or a Unix / Linux system.
'Yfpeng', you're welcome. How did you find us? Looks to me like you might be doing some bioinformatics work - analysing DNA sequences in FastA format? For readers who haven't come across these, they're strings of C A G and T characters that can be Megabytes long, but they come in files at 72 per line!
Posted by John_Moylan (jfp), 14 September 2002??How did you figure this boards latest member was from a bioinformatics background, just curious really as Perl seems to be "big" in this area, and I know nothing about it.
In fact, isn't there a book specifically for "Learning Perl for Bioinformatics" (erm, called that!)
Glad my stuff wasn't ripped apart, and hope it helped.
Posted by admin (Graham Ellis), 15 September 2002Ah ... it's quite unusual to want to go through a string of chacters one by one analysing each, even though you were forced to do so in older fashioned languages such as early "C" ... until higher level functions came along. You'll very often analyse sequences of numbers one by one (e.g. to add them for an average, so see if they're increasing, etc), but it's less common with characters. However, DNA and Amino Acid sequences are often represented as letters to be analysed one by one. Here's the description of Fasta format from http://www.ncbi.nlm.nih.gov/BLAST/fasta.html
So, "jfp, it's an educated guess on my part that this is a Bioinformatics question - please, original posted, can you confirm or deny my guess?
By the way - in a language like Java where you hold non-changing strings as Sting objects, and strings you're building up and manipulating as Stringbuffers, you (exceprionally) should use arrays of chars for handing sqeuence data such as FASTA.
Thinks ... now can I cross-post this to the Java board
Posted by yfpeng (yfpeng), 16 September 2002Graham is exactly right - I just started to do some bioinformatics work, I did not use Perl much before but it is certainly a very good language to process sequence data made of ACTG, etc.
I did not have a chance to test jfp's code yet, but it should work, thanks. I found this site from Perl.org. I will ask you questions in the future for sure.
Posted by yfpeng (yfpeng), 16 September 2002Jfp's codes works perfectly. Graham's guess is 100% correct. I am reading a FastA format file, so I need to remove the first line in the file, because the first line in a FASTA file is a description about the sequence below, rather than the sequence itself. I can not think of a way to do this in jfp's code. If I read the file into an array (not a scalar as jfp did), I can easily get rid of the first line by setting the first element of the array to be empty.
Posted by admin (Graham Ellis), 16 September 2002Read in the first line using something like
$header = <FH>;
It's often mis-stated that once $/ has been undef'd, the whole file is read when you do a <FH>. That's not quite true, as it reads all the rest of the file from where the current file pointer is - exactly what you need if the file contains a single FASTA sequence!
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho