seek_m:
Since you don't use indentation, it makes your code difficult to read. So the first thing I'd suggest is reformatting it so you can more easily see the structure. I'd next suggest using "use strict;" and "use warnings;" to help find possible problems (such as closing file handles you don't actually have open).
Then I'd look at the bits you don't understand, and figure out what they mean. For example, what do you want the if statement on line 29 (if(/(\d\_)+a1/.../(\d\_)+a1/)) to do?. Since it governs when you actually emit files, it would be a good place to start.
Finally, you may want to read about the perl debugger, as executing code under the debugger can be very helpful to both fixing your program as well as understanding how the code operates.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] [d/l] [select] |
can you suggest me a good perl debugger?
| [reply] |
There's a debugger already built into perl 5.x. Just add the "-d" parameter to run your program under the debugger. It'll take a little getting used to, especially if you're used to pointy clicky windows. But it does the job nicely.
It's documented in perldoc perldebug.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] [d/l] |
Excellent; some code to work with.
Okay, some basic coding structure issues first (and a point or two on style; I'm sorry, I can't help myself). Then we address your issue.
- Reformatted for readability. Please download the attached CODE from the bottom of this node. I will be using those line numbers, not your original ones, so please use it to follow along.
- Line 3: I strongly recommend you use strict; always. It will save you a LOT of hassle. With that one line of code, you just hired your Perl interpretter as a debugger, and it even works for free. It will seem like a husband or wife, continually finding fault with your work. But it will almost always be right, and you will become a better Perl programmer much more quickly.
- Line 20: You do opendir DIR .
Line 23: You do close DIR .
You have made this mistake in previous posts. open and close are matched; opendir and closedir are matched.
It's sort of like getting into your car through the window. You can open the window, and then close the window. Or you can break the window, and then repair the window. You don't open the window and then repair the window. These things are matched sets, and they need to be used as matched sets.
Side note: Stealing code (especially your own) is a time-honored tradition. You get something that works, you save it, and you re-use it to save yourself time and trouble.
However, since you made this exact same error in a previous script posted here, it seems you have re-used a broken piece of code instead of a working piece of code. You might consider adjustments to your process to reduce or eliminate the threat of this error in the future.
Might save you some time and hassle.
- Line 29: You do open filein,$filer;
It is common to make the bareword file handle in all caps so it stands out. Not required syntactically, but adviseable for readability.
I'd do: open FILEIN,$filer;
Note: If you do this, remember you'll have to change all your references from filein to FILEIN .
- Line 29: You open a file here but never close it.
- Line 36: Again, I'd uppercase the file handle.
- Line 36: You open a file here but never close it.
- Line 36: You claim the file you cannot open is called $out , but there is no $out . You would probably have caught this if you'd been using use strict; .
- Line 50: You do close $out; . But you never opened it. You would probably have caught this if you'd been using use strict; .
- Line 32: Here is the crux of your problem, once you clean up the rest of your script. Does this regular expression even work? I'm no expert on regular expressions, but this looks funky to me (which is humorous, given that regular expressions generally look funky anyway, but I digress).
I'd write a test script to confirm how the regular expression will function. Sample:
#!/usr/bin/perl -w
use strict;
my @TestInput =
(
'Line One',
'Line Two',
'Line Three',
'Line Four',
'Line Five',
);
foreach my $testLine (@TestInput)
{
print "-----> '$testLine'\n";
if(/(\d\_)+a1/.../(\d\_)+a1/)
{
my $var3 = "$outdir/$1 +to+ $2.txt";
print " Found '$var3'\n";
}
}
exit;
With your code structure repaired, use strict; at your side, and the behavior of your regular expression confirmed with the test script, see if you can make the code work.
And we'll all be here if you get stuck again. Just show us what you tried.
The reformatted script:
#!/usr/bin/perl -w
use strict;
{
print"enter the input directory path:\n";
chomp($indir=<STDIN>);
print"enter the output directory name:\n";
chomp($outdir=<STDIN>);
if ($indir eq $outdir)
{
print"you cannot have same input and ouput directory please
+change:\n";
exit();
}
else
{
chdir ("$indir") or die "$!";
opendir(DIR,".") or die "$!";
my @files=readdir DIR;
print @files;
close DIR;
foreach $file(@files)
{
unless (($file eq ".") || ($file eq "..") )
{
$filer="$indir/$file";
open filein,$filer;
while (<filein>)
{
if(/(\d\_)+a1/.../(\d\_)+a1/)
{
print;
$var3="$outdir/$1 +to+ $2.txt";
open filew,">>$var3" or die "cannot open
+ $out:$!";
print filew $_,"\n";
}
}
}
}
print"<----------------------------------------------->\n";
print "\t\t action done\n";
print "\a";
print "\a";
print "\a";
print"<----------------------------------------------->\n";
print"Results could be found in $outdir as txt files with TC
+ name\n";
print"<**********************..............*****************
+********>>\n";
close $out;
}
}
exit;
| [reply] [d/l] [select] |