I am parsing a very simple text file and manipulating it into an array. In my last step of the program I am trying to search for a user given term. I have tried many things but cannot seem to get it to work, my I search for a term that I know for a fact is in the array it still does not find it. I have a hunch that the variables might get tinkered with sometime during the “split”, “chop”, and “sort” functions but I am not exactly sure.
print "\n";
print "Enter a term you would like to search for: \n";
my $element = <STDIN>;
$num = grep(/$element/,@final);
if($num > 0){
print ("Term: " . $element . " found " . $num . " times. \n");
}else{
print "Not found. \n";
}
So I know for a fact that the term “apple” is in the list, when I search the term “apple” it prints that it was not found. Help please!!
You should try
Because your standard input will have a trailing newline that otherwise gets matched in your search. E.g.:
You should also be aware that meta characters will be interpreted in your input. So for example,
foo?will be considered “fo followed by an optional o”. To avoid that you can usequotemetaor\Q ... \E, e.g. in your regex use: