471,830 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,830 software developers and data experts.

file writing

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


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:


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. ..
  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.                  }
  10. close FH;
  11. close FHO;
  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 1154
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?

Nov 21 '07 #2
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
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

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.