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:
26005000
205300
405500
806000
1005500
300 05100
205200
405500
805600
1005800
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
205300
405500
806000
1005500
300 05100
205200
405500
805600
1005800
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/