PDA

View Full Version : Compare 2 arrays and return matches


bourbonmaster
12-15-2003, 10:04 PM
Please o please will some guru show me the light.

I have it all working up to this point.
I want to look for tld extensions (com org net whatever)
in a user submitted domain name and check for duplicates/matches of the extensions.
Part of an error check sub.
They can submit a name like subdomain.com.myhost.com I just want to let them know they are, to make sure thats what they really want.


$count=0;
$domain = "\"$incomingfromform\""; #users input
$domain =~ s/\./\"\,\"/g; #make it ready for array
@dom = ("com","org","net"); #what I want to match
@userinput = ($domain); #look for match in here
foreach (@dom) {
if ($_ =~ @userinput) {
#### $count++ foreach match######
#### and return name of each $match###
print "your entry $incomingfromform has $count \.$match\'s in it. Is that what you want?";
}
}

As you can see, I am clueless....

Thanks so much. I know there is an easy way to compare two arrays, count and return the matches. I tried to manipulate some dupe snippets to no avail..

Thanks to anyone who can help....

dswimboy
12-16-2003, 05:42 PM
@userinput = split /\./, $incomingfromform; #splits the user input at "."; stores result in array
@domains = ("com","org","net"); # what I want to match
foreach $domain (@domains) {
foreach $input (@userinput) {
if ($input =~ /$domain/) { count++; }
}
if ($count > 1) {
print "your entry $incomingfromform has $count \.$domain\'s in it. Is that what you want?";
}
}

this should do it. i'll check my books to see if you have to iterate over both arrays or not.

dswimboy
01-06-2004, 05:57 PM
i discovered an new way to solve your problem:


$input; # get variable from form
@domains = ("com","org","net"); # tld's
foreach $domain (@domains) {
if ($input =~ /\.($domain).*\.$1/i) {
print "your entry $input has multiple \.$domain's in it. Is that what you want?";
}
}