Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with perl 1

Status
Not open for further replies.

ksdh

Technical User
Oct 24, 2007
41
IN
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);
 
If I understand you correctly, then all you need to do is wrap a for loop around the open and close statements, i.e.

Code:
#!/usr/bin/perl
use strict;

# These are the configurations that you requested
my %config = (
  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
);

my $num_files = 10;
for (my $i = 0; $i < $num_files; $i++) {
    # not sure what you want the new filename to be
    my $filename = $config{filename};
    $filename =~ s/(.*?)\./$1-$i\./;

    open (DATAFILE, ">$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); 
}

--
 
Thanks a million sycootgit. It work as i wanted it to
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top