473,503 Members | 5,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file writing

2 New Member
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 1193
eWish
971 Recognized Expert Contributor
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 Recognized Expert Specialist
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

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

Similar topics

48
8415
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
2
1342
by: Pierre Rouleau | last post by:
Hi all! I'm trying to extend the functionality of the file object by creating a class that derives from file. MyFile class re-implements __init__(), write(), writelines() and close() to augment...
8
9829
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
8
3279
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
4
9806
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
3
26239
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
4
3644
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
14
2779
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
2036
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
12
4685
by: glennanthonyb | last post by:
Hi The company I work for has finally woken up to data security on our field laptops. I'm writing something in C# that will allow remote deletion of sensitive data and I don't believe...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.