in reply to How to match specific character in a string?

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