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. -
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);
-
7 1756
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: -
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
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.
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
Could you make the changes in my script please.
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. -
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++;
-
}
-
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
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: - 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);
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dennis Roberts |
last post by:
I have a script to parse a dns querylog and generate some statistics.
For a 750MB file a perl script using the same methods (splits) can
parse the file in 3 minutes. My python script takes 25...
|
by: Robert V |
last post by:
Hi all, I could use some help programming on of my Perl script to handle
different submit buttons within the same form. Here is what I have so far.
A user goes to a Web form and inputs some data...
|
by: dpackwood |
last post by:
Hello,
I have two different scripts that do pretty much the same thing. The main
perl script is on Windows. It runs and in the middle of it, it then calls
out another perl script that then...
|
by: Julia Bell |
last post by:
I would like to run the same script on two different platforms. The
directory in which the script(s) will be stored is common to the two
platforms. (I see the same directory contents regardless...
|
by: FLOTServer |
last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor".
On the game server is log file which contains all of the data from the
players for that day (kills, deaths, etc...). I...
|
by: PzYon |
last post by:
Hey 2gether!
I'm trying to execute a PERL script on the web server when the user presses
a button in an ASP.NET Web Application using Visual Basic. The most obvious
solution for me seemed to be...
|
by: roadbai |
last post by:
Hi all,
This is the first time to post question here, hopefully experts of perl here can give me a hand, to be honest, I am kind of new to perl, and I am struggling with the "Out of memory" issue I...
|
by: MK |
last post by:
Hello,
I am new to XML and PERL and I have a few questions the answers to
which I need to complete a project. All your time and effort would be
highly appreciated. I have to make a small HTML page...
|
by: Louis |
last post by:
I am trying to use php to update a spreadsheet (MS Excel or OpenOffice
Calc) with data from MySQL.
I looked into PEAR::Spreadsheet_Excel_Writer if it will do the job. If I
understand it...
|
by: uzzi |
last post by:
I don't know how to make a php script to work via cron...I want to make it run daily...My server API is CGI/Fast CGI.I have hosting from godaddy and in the help section i found that i have to specify...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |