You could approach the regular expression from a different direction. Rather than building the regular expression over and over with the values from one list, just grab what you need in every case. Then, use that to check set membership.

This particular method trades the complexity of the big regexen in the previous replies for the complexity of a hash and an extra conditional in the loop ( which is only conditionally executed ). I haven't benchmarked which way will be faster. Since we're talking about the regex engine the version of perl and your data set would make a difference in the speed and memory use of the large regex anyway.

When you are wanting to check membership in a set, a hash is much faster than an array. You could even have your loop update which members have been seen and how many times if you like. I have that commented out below since it's not part of your stated problem.

#!/usr/bin/perl # This particular code is tested. use strict; use warnings; # my @CS_CHECKS = ( '00012345', 'D123470', '0000123450', '0000023456', '50000123990' ); my %B_CHECKS = ( '1234' => 0, '12345' => 0, '123990' => 0, '12399' => 0 ); my %seen; for my $cn ( @CS_CHECKS ) { if ( $cn =~ /^(?:0|5|6)0+(.*)$/ ) { my $bn = $1; if ( exists $B_CHECKS{ $bn } ) { print "$bn = $cn\n"; # $B_CHECKS{ $bn }++; } } }

BTW, it's not enforced by the language, but all-capital symbol names are usually reserved by convention for constants. Also, your text states you're building a list, but your code is checking to see if your lists match. I'm guessing your code is meant to check the lists after you build list 2 to make sure everything did get included?


In reply to Re: Merged list with regex matches by mr_mischief
in thread Merged list with regex matches by tcf03

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.