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 1794 numberwhun 3,509
Recognized Expert Moderator Specialist
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.
numberwhun 3,509
Recognized Expert Moderator Specialist
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++;
-
}
-
numberwhun 3,509
Recognized Expert Moderator Specialist
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
KevinADC 4,059
Recognized Expert Specialist
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 minutes. It
is enough of a difference that unless I can figure out what I did
wrong or a better way of doing it I might not be able to use python
(since most of what I do is parsing various logs). The main reason to
try python is I had to look at...
|
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 into a textarea box. Below
this box there are two buttons ... one that is labelled "Save & Exit" and
other that is labelled "Save and Preview" On submit of either button, the
form data is sent to the same perl script. What I need to have happen is...
|
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 should run on a Unix box I have. Both
scripts run ok, except for the part when Windows try's to call out the
Unix script. I have it set up where the Unix is mapped through a drive
letter and can drop stuff into the Unix box. It is going through another...
|
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 of which
platform I use to access the directory.)
Platform 1: perl is installed in /tps/bin/perl. CPAN modules are available
Perl is also installed in /usr/bin/perl Platform 1, but the modules are
not accessible with this version.
Platform...
|
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 have a perl script that runs
on my webserver, which is supposed to login to the gameserver and download
the log. The problem is that when it gets to the part where it needs to
download the file, it aborts. The gameserver FTP requires I use port 24...
| |
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 to use the 'Shell()'-command .
This didn't work: Either I get an "File not found" error, or I get no error
at all, but the PERL script isn't executed anyway. The strange thing is that
I managed to execute a .bat file for example. Why the hell...
|
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 met when running my scripts.
Could somebody pay attention to look into the below details to give some help?
Really appreciate!!!!!
Let me give deatail info:
OS: HP 3000 MPE/iX.
|
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 which would take
in as input the state code, of the 50 states belonging to the Unites
States of America. After the user enters the two letter code and
presses the "GO" button, a *.xsd file is parsed to retrieve
information about the entire state...
|
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 correctly, it basically creates spreadsheets, but not
opening an existing spreadsheet and then updating and saving it, which
is what I want to do.
Do you know if there is any package out there that can do what I described?
|
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 the interpreter manually...look what it says:
i've tried with /web/cgi-bin/php "$HOME/html/path/to/my/script.php as command and it doesn't work.
if i run the script in browser it works...what should i introduce as a command..and do i have...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |