in reply to Merged list with regex matches

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?