473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

help with perl script

3
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
7 1768
numberwhun
3,509 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
ksdh
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.
Sep 24 '08 #3
numberwhun
3,509 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
ksdh
3
Could you make the changes in my script please.
Sep 24 '08 #5
nithinpes
410 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
3,509 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
4,059 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

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

Similar topics

7
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...
1
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...
3
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...
1
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...
3
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...
3
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...
1
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...
2
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...
5
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.