On 30 Apr 2004, at 12:20 AM, zunsheng Jiao wrote:
Hi all,
I'm new on perl.
I need to separate a huge file to small files. It has three columns. If first column is a number , use this number as a file name (i. e., 260.dat and 300.dat for following sample), and then writing column 2 and columns 3 to this file. Following is a sample such file:
260 0 5000 20 5300 40 5500 80 6000 100 5500 300 0 5100 20 5200 40 5500 80 5600 100 5800 Please help.
John
If your data is tab delimited the following will work. If not the split line needs to be changed depending on how your data is arranged.
# Go through each line of the data foreach $line(<DATA>) { # Split the line at whitespace my ($file, $col1, $col2) = split(/\s+/,$line);
# If there's something in the $file column, it's a new file
if ($file) {
# close the current file (but don't die if there is none)
eval{ close(FILE) };
# open the new file or die if we can't
open(FILE, ">/Users/rickm/Desktop/test/$file.dat") or die("I can't open $file.dat: $!");
}
# Write the data to the file
print FILE $col1 . "\t" . $col2 . "\n";
}
# close the latest file:
eval{ close(FILE) };
__DATA__ 260 0 5000 20 5300 40 5500 80 6000 100 5500 300 0 5100 20 5200 40 5500 80 5600 100 5800
Senior Developer PrintSupply - Print Procurement & Supply Management 18 Greenaway Street VIC 3105 Tel: (03) 9850 3255 Fx: (03) 9850 3277 http://www.printsupply.com.au/