Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Yes, that helped! Here's one option that substitutes the "_" with ".+" for use in a regex (use "." if you want only one letter to match):

use strict; use warnings; my @array1 = qw (fil_ t_xt _erl); my @array2 = qw (Merlin file text fils perl filled); for my $stem (@array1) { my $re = $stem; $re =~ s/_/.+/; /\b$re\b/ and print "$stem: $_\n" for @array2; }

Output:

fil_: file fil_: fils fil_: filled t_xt: text _erl: perl

Only whole words are matched, as word boundires (\b) are used in the regex which, if omitted, will also match substrings within the dictionary words.

Hope this helps!

Update: Below is an updated version which adapts BrowserUk's preferred solution:

use strict; use warnings; my @array1 = qw (fil_ t_xt _erl); my @array2 = qw (Merlin file text fils perl filled); my $words = join ' ', @array2; for my $stem (@array1) { my $re = $stem; $re =~ s/_/./; print "$stem: $1\n" while $words =~ /\b($re)\b/g; }

Output:

fil_: file fil_: fils t_xt: text _erl: perl

If you want the "_" to be matched by more than one letter in the dictionary words, change the substitution to $re =~ s/_/\\S+/;.


In reply to Re^3: matching the words by Kenosis
in thread matching the words by venky4289

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2014-09-02 00:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (18 votes), past polls