It might not be the fastest way, but the way to do that which I find the more logical is to first go through the first file and store its search pattern as a regex in an array (because order matters), and then go through the second file and find which lines match. And to be able to use the first file to order the result, I "associate" each match to the corresponding search/regex. And the associative structure in Perl is hashes.

use Data::Dumper; my @file1 = split "\n", <<_FILE1_; # I can't have two __DATA__ section +s in the same file so here goes the first one ldt b05dcc00 mny b05can03*n0b5 b05mdd04*n9c9 _FILE1_ my @regexen; # Ordered list of search patterns for (@file1) { next unless m<\S>; # skip the blank lines s<\*><\\w*>g; # the * becomes \w* which means "any number of alphanu +m chars or _" push @regexen, $_; # we push the pattern at the end of the list } my %result; while (<DATA>) # for each line of file 2 { for $search (@regexen) # for each search pattern { push @{ $result{$search} }, $1 if /\b(\w*$search\w*)\b/; # we push + the line at the end of the matches of $search if it matches } } # let's print the result ! for $key (@regexen) # for each search pattern, in the right order { for $line (@{ $result{$key} }) # for each line this search pattern m +atched { print $line, "\n"; # we print it } } __DATA__ /* 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 */
b05ldt10ud0e0 b05dcc00ud0c0 b05mny00ud0b5 b05mny00ud0d3 b05mny00ud0m7 b05can03un0b5 b05mdd04un9c9

You should use Data::Dumper; and print Dumper \%result; to see what it's made of if you have trouble understanding what I did :)


In reply to Re: How to match specific character in a string? by Eily
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.