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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another opinion with my regexp, please

Status
Not open for further replies.

j0nxuser

Technical User
May 4, 2003
31
US
OK - I have a list of customer names from a database query.

For my immediate goal, I only need the first three (3) alphanumerics (A-Za-z0-9). I can exclude ALL other characters that may appear...

Smith Co. (would want "SMI")
A-B-C Co. (would want "ABC")
U_US INC. (would want "UUS")

I need to match the first three alphanumerics of the company name from database "A" to the first three alphanumerics of the company name from database "B". If they match, do "this", if they don't do "that"

After searching I think that I found something that works, but want to make sure that there are no wholes in my theory... :)

Code Snippets:
Database "A"
if ($prod_nm =~ /^([A-Za-z0-9]{3})/){
$prod_nm_substr=$1;
}

Database "B"
if ($fe_customer_name =~ /^([A-Za-z0-9]{3})/){
$fe_customer_name_substr=$1;
}

Thank you for you advice,
Mark
 
This is something you want to use for both databases, so refactor it out to a subroutine:
Perl:
#!/usr/bin/perl

use strict;
use warnings;

sub clean_name {
   my $name = shift;
   $name =~ s/[^A-Z0-9]//ig;
   return uc(substr($name, 0, 3));
}

while (<DATA>) {
   print clean_name($_), "\n";
}

__DATA__
Smith Co.
A-B-C Co.
U_US INC.
Use the regex to remove the unwanted characters (your example was close), then just chop off the front three characters and upper case it.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Excellent - Thank you - Let me adjust my code accordingly and re-test.

Thank you!!

Mark
 
That worked like a charm - thank you for the idea!

Mark
 
Alternative suggestion for the subroutine code, should be more efficient:

Code:
sub clean_name {
   my $name = shift;
   $name =~ tr/a-zA-Z0-9//cd;
   return uc(unpack("A3",$name));
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin - Excellent! Thank you for the suggestion. I'll need to brush on my tr and unpack.

:)

Mark
 
Is this school work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Nope - It's actually for work. Out IT dept rolled out some "enhancements" which ended up breaking a few other pieces of logic within our systems/apps. That said, I need to band-aid the problem until the next maintenance window...

Is that better or worse then what you where thinking?

:)

Mark


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top