469,307 Members | 1,926 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

file writing

2
Hi,thanks 4 the previous help.
my problem is I have some data like this in a file=

AB:110256
aattggcaatgc
AB:1569854
ttgcttgcaa
aatcgcttttaa
ttttaaatgc
AB:569587
atgccttcat
cctcggattaag

I need to develop a programme,which will create multiple files according to the AB number (like AB:569587 will be one file)
and put each sequence in their corresponding files.like:

ttgcttgcaa
aatcgcttttaa
ttttaaatgc

this sequence will be put in the AB:1569854 file.

I have so far made a code which makes files according to the AB numbers,that is:

Expand|Select|Wrap|Line Numbers
  1. ..
  2.  
  3. #!usr/bin/perl -w
  4. open(FH,"h.txt")or die "cant open file";
  5. @a=<FH>;
  6. foreach $line(@a){
  7. open(FHO,">$line") ;
  8.                  }
  9.  
  10. close FH;
  11. close FHO;
  12.  
  13. ..
But I really dont know how to put data to each file from the main file,according to the file no.and their corresponding sequence.
plz help
Nov 21 '07 #1
2 1130
eWish
971 Expert 512MB
Can you provide some actual data? Also, how is this data stored in the file, is it on one line or over multiple lines? What is separating the data?

--Kevin
Nov 21 '07 #2
KevinADC
4,059 Expert 2GB
personally I would like to see more effort on your part next time. This code is only tested with the sample data provided, may perform diferrently with real data:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;
  4. open(FH,"h.txt")or die "cant open file: $!";
  5. MAINLOOP: while (<FH>) {
  6.     if (/^(AB:\d+)/) {
  7.         chomp;
  8.         tr/:/-/d;# because : should not be in filenames I change it to -
  9.         open(FHO,">", $_) or die "Can't create file $_: $!";
  10.         while (<FH>) {
  11.             if (/^(AB:\d+)/) {
  12.                 close FHO; 
  13.                 redo MAINLOOP;
  14.             }
  15.             print FHO;
  16.         }
  17.     }
  18. }                   
  19. close FH;
  20. print "finished";
Nov 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

48 posts views Thread by Joseph | last post: by
2 posts views Thread by Pierre Rouleau | last post: by
8 posts views Thread by Brandon McCombs | last post: by
4 posts views Thread by Oliver Knoll | last post: by
3 posts views Thread by StGo | last post: by
4 posts views Thread by Doug | last post: by
3 posts views Thread by brook | last post: by
12 posts views Thread by glennanthonyb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.