help with perl script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ksdh
    New Member
    • Sep 2008
    • 3

    #1

    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.
    Code:
    use strict;
    
    # These are the configurations that you requested
    my %config = (
      field1 => 204041000000001,                     # The value that field 1 should start at
      iterations => 10000,                         # The number of iterations
      filename => 'events_27.dat'                # The path to the data file you are creating
    );
    
    open (DATAFILE, ">$config{filename}") || die ("Cannot create file");
    
    for (1..$config{iterations}) {
      # Choose a random entry from the alpha array
    
      # Print the standard data with the changing configurations
      print DATAFILE "$config{field1},apn,pdp-context-error,,27,200912090900123\n";
    
      # Increment the changing fields
      $config{field1}++;
    }
    
    close (DATAFILE);
    Last edited by numberwhun; Sep 24 '08, 11:59 AM. Reason: Please use code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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:

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

    Regards,

    Jeff

    Comment

    • ksdh
      New Member
      • Sep 2008
      • 3

      #3
      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.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • ksdh
          New Member
          • Sep 2008
          • 3

          #5
          Could you make the changes in my script please.

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Originally posted by ksdh
            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.
            Code:
            open (DATAFILE, "$config{filename}") or die "Cannot read file:$!";
            open(OUT,">file1.dat") or die "Cannot create file:$!";
            my $i=1;my $c=1;
            while(<DATAFILE>) {
            print OUT;
            if($i%1000 == 0 && $i != 10000) {
              close OUT;
              $c++;
              my $file="file".$c.".dat";
              open(OUT,">$file") or die "Cannot create file:$!";
            }
            $i++;
            }

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by ksdh
              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

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                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:

                Code:
                use strict;
                use warnings;
                my %config = (
                   num_of_files => 10,
                   field1 => 204041000000001, # The value that field 1 should start at
                   iterations => 1000, # The number of iterations
                   filename => 'events_27.dat' # The path to the data file you are creating
                );
                
                foreach my $num (1..$config{'num_of_files'}) {
                   open (my $DATAFILE, ">", "${num}_$config{filename}")
                	   or die ("Cannot create file ${num}_$config{filename}: $!");
                   for (1..$config{iterations}) {
                      print $DATAFILE "$config{field1},apn,pdp-context-error,,27,200912090900123\n";
                      $config{field1}++;
                   }
                   undef($DATAFILE);
                }

                Comment

                Working...