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.


🦛


In reply to Re: Finding a partial match by hippo
in thread Finding a partial match by jpys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.