• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

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

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
blnukem
Asked:
blnukem
1 Solution
 
sureshpCommented:
Hi,

Hope this is your requirement.

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

print "Array --> @urarray";
0
 
sureshpCommented:
slight change from prev post ..
if($key =~ m/optimiz(\S+)|sale(\S+)?/)
0
 
blnukemAuthor Commented:
The  $SearchFor variable word will change so the m/ needs to be applied to the variable.
0
 
geotigerCommented:

#!/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
 
ozoCommented:
$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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now