[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Search an array for a certain word phrase (part two)

Posted on 2006-03-21
5
Medium Priority
?
483 Views
Last Modified: 2010-05-18
I would like to add the ability to search for “variants” of a word in this code. So searching for the word (optimize sales) would also find “optimizing sales” and “optimized sale”.



$SearchFor = "optimize sales";

@Array = qw(access to information that helps them optimizing sales opportunities and stay focused on the right optimized sale in helping companies effectively optimize their sales strategies so that they can optimize sales);

$WordCount = () = "@Array" =~ /$SearchFor/g;

print $WordCount;
0
Comment
Question by:blnukem
5 Comments
 
LVL 1

Expert Comment

by:sureshp
ID: 16245906
Hi,

Hope this is your requirement.

@urarray =();
foreach $key(@Array) {
      if($key =~ m/optimiz(\S+)|sale(\S+)/) {
            push(@urarray,$key);
      }
}

print "Array --> @urarray";
0
 
LVL 1

Expert Comment

by:sureshp
ID: 16245919
slight change from prev post ..
if($key =~ m/optimiz(\S+)|sale(\S+)?/)
0
 

Author Comment

by:blnukem
ID: 16245989
The  $SearchFor variable word will change so the m/ needs to be applied to the variable.
0
 
LVL 12

Expert Comment

by:geotiger
ID: 16246930

#!/usr/local/bin/perl
#
my $SearchFor = "optimize sales";
my $re = qr/(optimiz|sale)/;

my @Array = qw(access to information that helps them
    optimizing sales opportunities and stay focused on the right
    optimized sale in helping companies effectively optimize
    their sales strategies so that they can optimize sales);
my @a = ();
map { push @a, $_ if $_ =~ /$re/; } @Array;

print "WordCount: $#a (@a)\n";

WordCount: 7 (optimizing sales optimized sale optimize sales optimize sales)
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16248082
$SearchFor = qr/optimiz\w+ sales?/;

@Array = qw(access to information that helps them optimizing sales opportunities and stay focused on the right optimized sale in helping companies effectively optimize their sales strategies so that they can optimize sales);

$WordCount = () = "@Array" =~ /$SearchFor/g;

print $WordCount;
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question