G'day WWq,

Here's my take on a solution:

$ perl -Mstrict -Mwarnings -le ' my @arr1 = qw{ldt b05dcc00 mny b05can03*n0b5 b05mdd04*n9c9}; my @arr2 = map { /: (\w+)/; $1 } ( "/* To start: b05afn10ud0b0 */ ", "/* To start: b05dcc00ud0c0 */ ", "/* To start: b05ldt10ud0e0 */ ", "/* To start: b05dcc10ud0i0 */ ", "/* To start: b05afn10ud0m0 */ ", "/* To start: b05afn10ud0s0 */ ", "/* To start: b05mny00ud0b5 */ ", "/* To start: b05mny00ud0d3 */ ", "/* To start: b05mdd04un9c9 */ ", "/* To start: b05ahn00ud0j5 */ ", "/* To start: b05mny00ud0m7 */ ", "/* To start: b05can03un0b0 */ ", "/* To start: b05can03un0b5 */", ); for my $re (map { s/[*]/.*?/; $_ } @arr1) { /$re/ && print for @arr2; } ' b05ldt10ud0e0 b05dcc00ud0c0 b05mny00ud0b5 b05mny00ud0d3 b05mny00ud0m7 b05can03un0b5 b05mdd04un9c9

I'll walk you through that, comparing my code with yours:

  1. @arr1: No change to your code.
  2. @arr2: Because of the requirement, "printing must be follow the sequence of File1" [sic], you'll need to run through this array multiple times. To avoid dealing with all that extra text ("/* To start:" and " */") on every iteration, I've filtered that out initially with a simple map. That's a minor change to your code.
  3. Outer loop (@arr1): I've added another simple map so we're now dealing with regular expression patterns. Again, a minor change to your code.
  4. Inner loop (@arr2): We've done most of the work by now so just print when there's a match. There didn't seem to be enough code left to warrant more than a single line. That's the biggest change to your code.

-- Ken


In reply to Re: How to match specific character in a string? by kcott
in thread How to match specific character in a string? by WWq

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.