I am creating a perl script that takes in the a file (example ./prog file)
I need to parse through the file and search for a string. This is what I thought would work, but it does not seem to work. The file is one work per line containing 50 lines
@array = < >;
print "Enter the word you what to match\n";
chomp($match = <STDIN>);
foreach $line (@array){
if($match eq $line){
print "The word is a match";
exit
}
}
You’re chomping your user input, but not the lines from the file.
They can’t match; one ends with
\nthe other does not. Getting rid of yourchompshould solve the problem. (Or, adding achomp($line)to your loop).or
Edit in the hope that the OP notices his mistake from the comments below:
Changing
eqto==doesn’t “fix” anything; it breaks it. You need to useeqfor string comparison. You need to do one of the above to fix your code.Output: