473,809 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with perl script

3 New Member
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 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:

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 New Member
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 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
Sep 24 '08 #4
ksdh
3 New Member
Could you make the changes in my script please.
Sep 24 '08 #5
nithinpes
410 Recognized Expert Contributor
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 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
Sep 24 '08 #7
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:

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
2210
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...
1
1963
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...
3
6560
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...
1
4694
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...
3
15286
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...
3
4990
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...
1
2526
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.
2
1726
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...
5
1779
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?
3
2349
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...
0
9603
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,...
0
10378
jinu1996
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...
1
10391
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,...
1
7664
isladogs
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...
0
6881
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();...
0
5550
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...
1
4333
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
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
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...

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.