467,179 Members | 1,197 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

help with perl script

HI
I am using the below script to create a file with 10000 iterations. What i want to do is, use a loop that would create a file with 1000 iterations for the 10000.
So basically i would like to break up the file with 10000 iterations using a loop to have 10 files with 1000 iterations each. It might be easy to implement, but i am still new to perl.
Any help is appreciated.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. # These are the configurations that you requested
  4. my %config = (
  5.   field1 => 204041000000001,                     # The value that field 1 should start at
  6.   iterations => 10000,                         # The number of iterations
  7.   filename => 'events_27.dat'                # The path to the data file you are creating
  8. );
  9.  
  10. open (DATAFILE, ">$config{filename}") || die ("Cannot create file");
  11.  
  12. for (1..$config{iterations}) {
  13.   # Choose a random entry from the alpha array
  14.  
  15.   # Print the standard data with the changing configurations
  16.   print DATAFILE "$config{field1},apn,pdp-context-error,,27,200912090900123\n";
  17.  
  18.   # Increment the changing fields
  19.   $config{field1}++;
  20. }
  21.  
  22. close (DATAFILE);
  23.  
Sep 24 '08 #1
  • viewed: 1488
Share:
7 Replies
numberwhun
Expert Mod 2GB
Ok, so we know where you stand, what is happening when you run this? Are there errors? Does it do anything that you expect?

Also, at the end of your die functions, it is always extremely helpful to add the $! variable as it contains the error received during the command preceding. Here is an example:

Expand|Select|Wrap|Line Numbers
  1. open(FILE, ">myfile.txt") or die "Cannot open file for writing:  $!";
  2.  
On one more note, please use code tags around ANY code that you place in the forums.

Regards,

Jeff
Sep 24 '08 #2
Thanks for the suggestion.
But my problem here is, like i explained earlier, i have one file that creates 10k records, i would like to break up that one file, have 10 files instead with 1k records each. Ii might involve using a for loop but i am just a bit confused as to how to implement it.
Sep 24 '08 #3
numberwhun
Expert Mod 2GB
Well, you already said you were going to read the 10k line file, line by line. So, why not use a counter that gets iterated at the end of a loop, yet at the beginning of the loop, you could check if the counter is equal to 1000. If it is, then you could call a function that opens a new file. However you do it, its just an idea.

Regards,

Jeff
Sep 24 '08 #4
Could you make the changes in my script please.
Sep 24 '08 #5
nithinpes
Expert 256MB
Could you make the changes in my script please.
You may try to optimize this. Appending the following script to your original script will do the job.
Expand|Select|Wrap|Line Numbers
  1. open (DATAFILE, "$config{filename}") or die "Cannot read file:$!";
  2. open(OUT,">file1.dat") or die "Cannot create file:$!";
  3. my $i=1;my $c=1;
  4. while(<DATAFILE>) {
  5. print OUT;
  6. if($i%1000 == 0 && $i != 10000) {
  7.   close OUT;
  8.   $c++;
  9.   my $file="file".$c.".dat";
  10.   open(OUT,">$file") or die "Cannot create file:$!";
  11. }
  12. $i++;
  13. }
  14.  
Sep 24 '08 #6
numberwhun
Expert Mod 2GB
Could you make the changes in my script please.
I'm sorry, but this is not a scripting service. We are all volunteers and here to help you. By help, I mean that you must produce the code, or at least try, and we will push you in the right direction.

Although nithinpes gave you some code, I did give you the nudge I thought you needed and was hoping to see you write some code. If you had, I would have been more than happy to help you with it.

Regards,

Jeff
Sep 24 '08 #7
KevinADC
Expert 2GB
Since code has already been posted here is another way it could be done, of course this code is untested since I am not going to create 10 files with 1000 lines each to see if it works as expected:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my %config = (
  4.    num_of_files => 10,
  5.    field1 => 204041000000001, # The value that field 1 should start at
  6.    iterations => 1000, # The number of iterations
  7.    filename => 'events_27.dat' # The path to the data file you are creating
  8. );
  9.  
  10. foreach my $num (1..$config{'num_of_files'}) {
  11.    open (my $DATAFILE, ">", "${num}_$config{filename}")
  12.        or die ("Cannot create file ${num}_$config{filename}: $!");
  13.    for (1..$config{iterations}) {
  14.       print $DATAFILE "$config{field1},apn,pdp-context-error,,27,200912090900123\n";
  15.       $config{field1}++;
  16.    }
  17.    undef($DATAFILE);
Sep 24 '08 #8

Post your reply

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

Similar topics

7 posts views Thread by Dennis Roberts | last post: by
1 post views Thread by Robert V | last post: by
1 post views Thread by Julia Bell | last post: by
3 posts views Thread by FLOTServer | last post: by
2 posts views Thread by MK | last post: by
5 posts views Thread by Louis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.