CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Question about merging two files in perl

 



JackieZhou
New User

Jan 20, 2013, 1:40 PM

Post #1 of 5 (980 views)
Question about merging two files in perl Can't Post

I have two files, file1 is as below:

@HW-ST997:122
AGAT
+
#11B
@HW-ST997:120
GATC
+
#BCI
@HW-ST997:112
AGCT
+
#
:BDI
---------------------
Explanation: every four lines represent a sequence, the first line is the head (ID) of that sequence.

I have file2 as below:
@HW-ST997:120
@HW-ST997:112
@HW-ST997:110
@HW-ST997:108
------------------------
Explanation: In file2, there are IDs of the sequences.

What I want to do is to keep only the sequences in file1 whose IDs can be found in file2. Notice, I want to keep the whole thing, I mean, not only the IDs, but also the 2nd, 3rd, 4th lines for each sequence information.

How to implement that in perl?

Thank you very much,

Jackie


7stud
Enthusiast

Jan 20, 2013, 2:56 PM

Post #2 of 5 (977 views)
Re: [JackieZhou] Question about merging two files in perl [In reply to] Can't Post

Read each line in file2, and enter the ids as the keys in a hash, the values don't matter (undef is a good choice).

Read 4 lines from your file. Check whether the id in the first line is in the hash:

Code
my $id = $lines[0]; 

if ( exists $ids{$id} ) {
#write the 4 lines to an output file
}



(This post was edited by 7stud on Jan 20, 2013, 3:01 PM)


JackieZhou
New User

Jan 20, 2013, 3:12 PM

Post #3 of 5 (971 views)
Re: [7stud] Question about merging two files in perl [In reply to] Can't Post

Thanks a lot for the reply, but I am really a newbie in terms of perl programming. Can anyone show me some sample code that I can use and learn from?

Thanks a lot!!

-Jackie


Rahul6990
Novice

Jan 20, 2013, 11:26 PM

Post #4 of 5 (944 views)
Re: [JackieZhou] Question about merging two files in perl [In reply to] Can't Post

Read programming Perl o'reilly book.Its really great for beginners.


7stud
Enthusiast

Jan 20, 2013, 11:54 PM

Post #5 of 5 (941 views)
Re: [Rahul6990] Question about merging two files in perl [In reply to] Can't Post

"Programming Perl" is NOT good for beginners. Read "Learning Perl 6th Ed.".


(This post was edited by 7stud on Jan 20, 2013, 11:54 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives