Aug 19, 2014, 1:11 AM
Post #1 of 3
I ran through the FAQs and as many places as I could before posting here. Sorry if I missed the answer.
Odd Grep/Regular Expression issue
I am currently building a large data set. Each value in the array I built contains several paragraphs of information. I set out to build a regular expression that would search each value in the array and return it if the proper keyword was found. They keyword was requested when you run the program.
I believe that's where the problem lies. Ignoring as much data as I feel I can (the array is huge), to search for the data, I have the code
#@charm is the array the information is stored in.
say "What is the keyword that we must search for?";
my $keyword = <ARGV>;
my @selected = grep (/$keyword/, @charm);
print Dumper @selected;
Okay. When I first ran this, it seemed to work wonderfully and I left it alone. However, when I tried to run it on a different keyword that I knew was there, it couldn't do it. I ran more tests and I realized the regular expression is looking at the last characters of each new line in the string, aka the paragraphs.
Example. If the array value looked like this:
"Mary had a little lamb
whose fleece was white as snow.
And everywhere she went
People would say hello lamb."
and I searched for lamb I would get this value once as lamb showed up at the end of the first line. If I instead searched for everywhere, this value wouldn't appear.
I assume this is because grep runs a regular expression search and $ is a variable that indicates the end of a line.
I don't know how to get around this problem. Thoughts?
Est Sularus oth Mithas
Just learning coding. 24 years old, working on learning CS for my graduate program.