Perl Question (programming code)?

ninjakidd

New member
For some reason when it searches reads every other line.... i can't figure out why:

#!/usr/local/bin/perl



print "type word to search for: ";
$line=<STDIN>;

print "The file reads:\n";
open (MYFILE, 'test.txt');
while (<MYFILE>)
{
chomp;
print "$_\n";


}
close (MYFILE);



open (MYFILE, 'test.txt');
while (<MYFILE>)
{
chomp;
if (<MYFILE> =~ /$line/)
{
print "$_";
print ", It matches\n";
}
else
{
print "$_";
print ", It doesn't match\n";
}


}
 
i'm no perl guy, but

your while(<MYFILE>) probably goes to the next line, and so does the if statement...

change the if statement so it only accesses the line, instead of using <MYFILE>

good luck, I hope i could help, i don't know perl :(
 
1. Add "use strict;" at the top
2. Add "use warnings;" at the top
3. Rename your variable $line to $word, that's what it is
4. Do not rely on the $_ variable, declare your own: while (my $sLine = <MYFILE>) ...
5. You do not need to chomp the line in order to do the match
6. Compare on the line you already read, don't read another line: if ($sLine =~ m/$word/) ...

I could go on, but this should get you started.
 
Back
Top