perl array matching between area code and no.

Hi Everyone,

I have:

my @No=qw(032106 032630 0380 034010 035110 0354801111);
my $str_No=join(';', @No);

I have a string $strA="03263033", so in order to determine this $strA area code matches with @No, I can do:

if ( (rindex($str_No,substr($strA,0,5))))== -1) ) {
print "Not exist.";
}

I have a string $strB="0380112", as we can see this area is just "0380", this strB is inside the @No. But at this time the substr is from 0 to 3.

But the problem is below:
my @No=qw(0321063033 0380112 03548011112) this list maybe 100+,1000+
It is imporssible to do it one by one, any simple perl way? Please advice.

Thanks

The "area" you describe is not familiar to me. How do you know that 0380 is the area part of 0380112? Is it because it starts and ends with zero? If so, and the areas are of varying length, you can use a regexp to extract the area:

$n = '0380112';
($area) = $n =~ (/^(0[^0]+0)/);
print $area;  

Then you can use $area to search over the list. I see no reason to join the list/array into a string to search it but you can if you wanted to for some reason I am not aware of.

---------- Post updated at 12:01 PM ---------- Previous update was at 11:50 AM ----------

my @No=qw(0321063033 0380112 03548011112);
my $n = '0380112';
my ($area) = $n =~ (/^(0[^0]+0)/);
my $flag = 1;
for (@No) {
   if (/^$area/) {
      $flag = 0;
      last;
   }
}
if ($flag) {
    print "$area not found\n";
}
else {
    print "$area found\n";
}