Regular expression – Assess postal code input to Perl array

I have an array containing the first 2 characters of the postal code area in Perl, as shown below:

@acceptedPostcodes = ("CV", "LE", "CM", "CB", "EN", "SG", "NN", "MK", ​​"LU", "PE", "ST", " TF", "DE", "WS");

I have a search box where users can enter partial or complete zip codes. I need to check whether the postcode they enter is in an array For example, if they enter’CV2 1DH’, it will evaluate to true, if they enter something like’YO1 8WE’, it will evaluate to false. It does not start with one of the array values.

Now this is very easy for me to do with PHP, but Perl is not something I am very good at, and my efforts so far have not been very effective.

Any thoughts on peeking ?

If the list of postal codes you accept is large enough that the performance in the matching code is a practical Question (probably not), then you’d better use hash lookup instead of array anyway:

#!/usr/bin/perl
< br />use strict;
use warnings;

my %accepted_postcodes = ("CV" => 1, "LE" => 1, "CM" => 1, "CB" => 1, "EN" => 1, "SG" => 1, "NN" => 1, "MK" => 1, "LU" => 1, "PE" => 1, "ST" = > 1, "TF" => 1, "DE" => 1, "WS" => 1);
# Or, to be more terse:
# my %accepted_postcodes = map {$_ => 1} qw(CV LE CM CB EN SG NN MK LU PE ST TF DE WS);

my $postcode = "CV21 1AA";

if (exists $accepted_postcodes{substr $postcode, 0, 2}) {
print "$postcode is OK " ;
} else {
print "$postcode is not OK ";< br />)

This method is suitable for 5.8.8.

I have an array containing the start of the postal code area in perl 2 characters, as shown below:

@acceptedPostcodes = ("CV", "LE", "CM", "CB", "EN", " SG", "NN", "MK", ​​"LU", "PE", "ST", "TF", "DE", "WS");

I have a search box, users You can enter a partial or complete zip code. I need to check whether the post code they enter is in one element in the array For example, if they enter’CV2 1DH’, it will evaluate to true, if they enter something like’YO1 8WE’, it will evaluate to false. It does not start with one of the array values.

< p>Now this is easy for me to do with PHP, but Perl is not something I am very good at, and my efforts so far have not been very fruitful.

Any thoughts on peeking?

If the list of postal codes you accept is large enough that performance in the matching code is an actual issue (and may not be), then you are best anyway Use hash search instead of array:

#!/usr/bin/perl

use strict;
use warnings ;

my %accepted_postcodes = ("CV" => 1, "LE" => 1, "CM" => 1, "CB" => 1, "EN" => 1, " SG" => 1, "NN" => 1, "MK" => 1, "LU" => 1, "PE" => 1, "ST" => 1, "TF" => 1, "DE "=> 1, "WS" => 1);
# Or, to be more terse:
# my %accepted_postcodes = map {$_ => 1} qw(CV LE CM CB EN SG NN MK LU PE ST TF DE WS);

my $postcode = "CV21 1AA";

if (exists $accepted_postcodes{substr $postcode, 0, 2}) {
print "$postcode is OK " ;
} else {
print "$postcode is not OK ";
}

This This method is applicable to 5.8.8.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5456 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.