in reply to There is more than one way (and mine is not the best)

Updated with a little more code and comments.

Is there some reason you are not using File::Find for this?

I have added some sample, untested code for you below. I have no idea what your code is doing, but I hope the following will help you do it better.

#!/usr/bin/perl -w use strict; use File::Find; my $dir = "/Wherever my .txt files are stored"; my ($qnum, $code, $ctext, $ntext, $stext) = ('', '', '', '', ''); find(\&processfile, ("$dir/")); sub processfile { if (/(.*)\.txt$/i) { open QIN, "$_" or die $!; open QOT, ">$1.txt" or die $!; while (<QIN>) { chomp; s/\222/'/g; s/\226/-/g; if (/^Col/i) { s/^col.//gi; s/\(net\)//gi; $ntext = $_; print QOT "net1$ntext (net);unl1\n"; } elsif (/^\(S/i) { (undef, $stext) = split(/\s/, $_, 2); print QOT "net2$stext (subnet);unl1\n"; } elsif (/^[0-9]/) { ($code, $ctext) = split(/\s/, $_, 2); print QOT "n01$ctext;c=(&txt($code.ge.1)\n"; } } close QIN; close QOT; open (QIN, "$1.tmp") or die $!; open (QOT, ">$1.axe") or die $!; while (<QIN>) { if (/n01\s+nothing|n01\s+don''t know|n01\s+other/i) { $_ =~ s/\n/;nz;nosort\n/g; } if (/\s+[\w;]/) { s/n01\s+/n01/g; s/net1\s+/net1/g; s/net2\s+/net2/g; } if (/^net1misc/i) { s/unl1/unl1;nz;nosort/g; } unless (/^n01;c=|^net. /i) { print QOT $_; } } close QOT; } }

Replies are listed 'Best First'.
Re: Re: There is more than one way (and mine is not the best)
by graff (Chancellor) on May 26, 2004 at 04:43 UTC
    Is there some reason you are not using File::Find for this?

    Well, it seems that one very good reason is that the OP doesn't show any need for recursive descent of a directory tree. All the files are in a single directory, and File::Find is overkill for that -- and somewhat harder to grok (because of all the unrelated things it does), compared to the simple glob example in the first reply, or the basic "opendir...; readdir..." functions, which are also much easier to learn than File::Find -- e.g.:

    opendir( D, "." ) or die "WTF? $!"; for my $file ( grep /\.txt$/, readdir D ) { # do whatever it is the OP is doing... }
    For that matter, the OP didn't say one way or the other, but maybe there are subdirectories containing "*.txt" files, and he might actually prefer to leave those alone -- just process the ones in the current directory. In that case, File::Find would cause real trouble (or at least extra work to avoid trouble).