while (my $line = <$original_fh>) {
for my $file (@files) {
print "Enter the desired number of columns: ";
my $nr = <STDIN>;
chomp($nr);
$line =~ s/((\w\w\t){$nr})/$1\n/g;
}
If you translate this in plain english it sounds like: while is true that I can read a line from this file, for each file I have, ask me how many column I must consider. It does make no big sense.
You want firstly ask how many column to consider, then for each file obtain the output filename, open it and foreach line do some transformation. Right?
Now in plain english (if my english can be plain..) your overall program must resemble to something like this TODO list:
- Fill @files using glob
- decide how many column are desired (ask the user or hardcode in the program)
- foreach @files compose the name of the output file ( my $new_file = $name =~ s/txt$/csv/r; ), open a new file in in write mode open( my $final_fh, ">", $final ) or die $!; then open the source file for reading and foreach line make some transformation and write the line to the output file. When input file is finished, clean up ie close the pair of files and pass to next source one.
When you have the basic flow of the program working you can add some brick here and there to make it more robust: what happen if I cant read? you yet added or die $! and this is good. What happen if an output file already exists?
Notice also that a practical way to ask user for something can be accomplished in a cleaner and less verbose way giving your program the ability to parse command line arguments: two core module are devoted to such tasks: Getopt::Std that is basic and the more developped Getopt::Long
HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|