Hi Birgit,
it does work the way it is now, it only doesn't know what to do if there is no result, as the way I've got it now that can be the case...
I'll show the complete (modified) code:
Code:
sub get_nearest_postal_code {
# --------------------------------------------------------
# Postcode script, to find the nearest company and send the result to html_record
&html_print_headers;
my $inputnumber = $in{'Postcode'}; # Postcode = name of form field
my (@Postcode, %difference, %letters, @nearestnumber, %id);
open (DB, "<$db_file_name") || &cgierr ("can't open $db_file_name: $!");
my $pos = $db_def{'Postcode'}[0];
my $id_pos = $db_def{'ID'}[0]; # assuming "ID" is the name of your ID field
my $Discipline_pos = $db_def{'Discipline'}[0];
while (<DB>) {
chomp;
my @line = split/\|/, $_;
# if ($line[$Discipline_pos] eq $in{'Discipline'}) { # exact match
if ($line[$Discipline_pos] =~ m/$in{'Discipline'}/) { # if it contains
push (@Postcode, $line[$pos]);
$id{$line[$pos]} = $line[$id_pos];
# print "Record ID in Hash: $id{$line[pos]}, Record ID: $line[$id_pos], Postcode: $line[$pos]<br>";
}
}
foreach my $Postcode (@Postcode) {
if ($Postcode =~ /(\d+)(\s)(\w+)/) {$Postcode = $1; $letters{$Postcode} = $3;}
$difference{$Postcode} = abs($Postcode-$inputnumber);
}
foreach my $key (sort {$difference{$a} <=> $difference{$b} } keys %difference) {
push (@differencearray, $difference{$key});
}
foreach my $key (keys %difference) {
if ($difference{$key} == $differencearray[0]) {
$key = "$key $letters{$key}";
push (@nearestnumber, $key);}
}
# foreach my $nearestnumber (@nearestnumber) {
# print "Dichtstbije postcode van $inputnumber is: $nearestnumber\n<p>";
# print "$id{$nearestnumber}";
# }
foreach my $nearestnumber (@nearestnumber) {
my %rec = &get_record($id{$nearestnumber});
# &html_record(%rec);
print qq|
<html>
<head>
<title>$html_title: Search the Database.</title>
<META HTTP-EQUIV="refresh" CONTENT='0;url=$build_root_url/$id{$nearestnumber}.shtml'>
</head>
<body bgcolor="#ffffff">
</body>
</html>
|;
}
}
Thanks for again taking the time to look at this!
Lex