Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to say, zip code Starts With 2

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
0
0
US
hello,

I am more of an SQL person - so what I am trying to say, I am not sure how to say in Perl. I am sure it is some sort of regular expression. Maybe YOU can help me!

I would like to say,

if ($ZipPlus4 STARTS WITH 29626) then increment n.

any hints?

thank you!
 
You don't really need a regexp to search for simple sub-strings within strings.

Code:
$n++ if (index $ZipPlus4,'29626') == 0;

'0' (zero) means the location of the sub-string is at the beginning of the string.

- Kevin, perl coder unexceptional!
 
That's true Kevin. You could also use substr as well:

$n++ if substr($ZipPlus4, 0 , 5) eq '29626';

Overall though, I think the deciding factor should be which code is the most readable. My first example directly translates to "increment $n if $ZipPlus4 begins with '29626'". At least it does if you speak Regular Expression :)

I went ahead and did a quick benchmark, and these were my results:

Code:
Benchmark: timing 5000000 iterations of index, regexp, substr...
     index: 14 wallclock secs (12.51 usr +  0.00 sys = 12.51 CPU) @ 399424.83/s
(n=5000000)
    regexp: 12 wallclock secs (11.03 usr +  0.00 sys = 11.03 CPU) @ 453103.76/s
(n=5000000)
    substr: 16 wallclock secs (14.72 usr +  0.00 sys = 14.72 CPU) @ 339650.84/s
(n=5000000)

It should be noted though that this was with only 9 character strings. If the string were any longer I believe index would end up much slower as it would scan through the entire string for false tests.

Back to real coding fun.
 
Thank you much for the tips! This worked great. So, once again coming from the SQL point of view - what if i have SEVERAL zip codes that i'm looking for? what if i want to say, If $ZipPlus4 has a value in ('29626', '29627', '29628', '29788') then increment $n += 1, and if it's NOT in those values, then increment $o += 1.

any help is GREATLY appreciated!

~polka
 
In that instance, the best method is to turn your $ZipPlus4 to a simple $Zip (as in only 5 characters) and then do a direct search through your array of zips.

Code:
my $zip = substr($ZipPlus4, 0, 5); # Only first 5 numbers

if (grep {$zip eq $_} qw(29626 29627 29628 29788)) {
    $n++;
} else {
    $o++;
}

Alternatively, you could also use a %hash lookup table. In fact, that is the fastest method. Especially if you are doing lots of lookups:

Code:
my $zip = substr($ZipPlus4, 0, 5); # Only first 5 numbers

my %isSpecialZip = map {$_ => 1} qw(29626 29627 29628 29788);

if (isSpecialZip{$zip}) {
    $n++;
} else {
    $o++;
}

Either method works of course. It's important to know both though since grep works great for one-liners, which are often very useful.
 
Slight bug in my second bit of code. Forgot the dollar sign:

Code:
...
if ([COLOR=red]$[/color]isSpecialZip{$zip}) {
...
 
Yep, I have to give you this one Miller. Since the pattern is bound to the beginning of the string the regexp will be faster. [thumbsup]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top