I have tried to use m// as well as grep but if the string isn't the exact same word in the string and array it won't be found.
If you have code which runs but doesn't do quite what you want then it would be very useful if you were to provide that here in the form of an SSCCE - that way we can be sure we know what it is you are trying to achieve. The verbose description is OK but I'm finding it hard to understand quite what you have and quite what you want it to do.
I think you want to treat the array items as substrings for the purposes of matching against "the string". So, here is a test script to get you started in that vein.
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 2;
my @x = qw/catch cat foo nocat/;
my $str = '2cat6';
my @found = grep { -1 < index $str, $_ } @x;
my @want = qw/cat/;
is_deeply \@found, \@want, "Found '@found' in '$str'";
$str = '2catch6';
@found = grep { -1 < index $str, $_ } @x;
@want = qw/catch cat/;
is_deeply \@found, \@want, "Found '@found' in '$str'";
This code uses Test::More to run 2 specific tests. In each one, the desired outcome is held in @want and this is compared with the generated array of matches stored in @found. If these aren't the right tests then you can amend the script as you see fit. See How to ask better questions using Test::More and sample data for more on using tests in this way.
|