O
one man army
Hi All-
I am an infrequent user of Perl. I sat down this morning to do a
simple task, which started with referring to some data. It took me a
while to write this simple routine, making most every mistake on the
way. Perhaps it will be useful to someone.
I tried to use qw( "name" "name space" ); and had problems, so I use the
long form. Constructive comments welcome.
The array is almost a thousand elements actually, edited for usenet
<- snip ->
#! /usr/bin/perl -w
use strict;
use warnings;
my @CA_Cities = (
"adelanto", "agoura hills", "alameda", "alamo", "albany", "alhambra",
"newman", "newport beach", "nipomo", "norco", "north auburn",
"north highlands", "norwalk", "novato", "oakdale", "oakland",
"oceanside", "oildale", "ojai", "olivehurst", "ontario", "opal cliffs",
"orange cove", "orangevale", "orcutt", "orinda", "orland", "orosi",
"oroville east", "oxnard", "pacific grove", "pacifica", "palm desert",
"palmdale", "palo alto", "palos verdes estates", "paradise", "paramount",
"parkway-south sacramento", "parlier", "pasadena", "patterson",
"petaluma", "pico rivera", "piedmont", "pinole", "pismo beach",
"placentia", "placerville", "pleasant hill", "pleasanton", "pomona",
"porterville", "portola hills", "poway", "prunedale", "quartz hill",
"ramona",
"rancho cordova", "rancho cucamonga", "rancho mirage",
"rancho san diego", "rancho santa margarita", "red bluff", "redding",
"redlands", "redondo beach",
"redwood city", "reedley", "rialto", "richmond", "ridgecrest",
"rio del mar", "ripon",
"west whittier-los nietos", "westlake village", "westminster",
"westmont",
"whittier", "wildomar", "willowbrook", "willows", "windsor", "winter
gardens","winters", "winton", "woodcrest", "woodlake", "woodland",
"yorba linda", "yreka", "yuba city", "yucaipa", "yucca valley"
);
sub match_City {
my( $findme ) = @_;
my( $start, $end ) = ( 0, $#CA_Cities );
my( $idx );
my $cnt = 0;
# convert to lowercase
$findme =~ s/([A-Z]+)/\L$1/g;
print "\nSEARCH\n";
print "findme= " . $findme . "\n";
while ( $start != $end && $cnt < $#CA_Cities) {
#note start and end never become equal in alot of cases
$idx = int (( $end - $start ) / 2) + $start;
print "a= " . $CA_Cities[ $idx ] . " ";
if ( $CA_Cities[ $idx ] eq $findme ) {
return $findme;
} elsif ( $CA_Cities[ $idx ] gt $findme ) {
$end = $idx;
} elsif ( $CA_Cities[ $idx ] lt $findme ) {
$start = $idx;
}
print "idx= " . $idx . " start= " . $start . " end= " . $end . "
cnt= " . $cnt ."\n";
$cnt++;
}
return "not found\n";
}
## Test this
print "\n" . match_City( "ripon" );
print "\n" . match_City( "riPoN" );
print "\n" . match_City( "Ripon" );
print "\n" . match_City( "yorba linda" );
print "\n" . match_City( $CA_Cities[ int(rand $#CA_Cities) ] );
print "\n" . match_City( $CA_Cities[ int(rand $#CA_Cities) ] );
print "\n" . match_City( $CA_Cities[ int(rand $#CA_Cities) ] );
print "\n" . match_City( "Fred" );
I am an infrequent user of Perl. I sat down this morning to do a
simple task, which started with referring to some data. It took me a
while to write this simple routine, making most every mistake on the
way. Perhaps it will be useful to someone.
I tried to use qw( "name" "name space" ); and had problems, so I use the
long form. Constructive comments welcome.
The array is almost a thousand elements actually, edited for usenet
<- snip ->
#! /usr/bin/perl -w
use strict;
use warnings;
my @CA_Cities = (
"adelanto", "agoura hills", "alameda", "alamo", "albany", "alhambra",
"newman", "newport beach", "nipomo", "norco", "north auburn",
"north highlands", "norwalk", "novato", "oakdale", "oakland",
"oceanside", "oildale", "ojai", "olivehurst", "ontario", "opal cliffs",
"orange cove", "orangevale", "orcutt", "orinda", "orland", "orosi",
"oroville east", "oxnard", "pacific grove", "pacifica", "palm desert",
"palmdale", "palo alto", "palos verdes estates", "paradise", "paramount",
"parkway-south sacramento", "parlier", "pasadena", "patterson",
"petaluma", "pico rivera", "piedmont", "pinole", "pismo beach",
"placentia", "placerville", "pleasant hill", "pleasanton", "pomona",
"porterville", "portola hills", "poway", "prunedale", "quartz hill",
"ramona",
"rancho cordova", "rancho cucamonga", "rancho mirage",
"rancho san diego", "rancho santa margarita", "red bluff", "redding",
"redlands", "redondo beach",
"redwood city", "reedley", "rialto", "richmond", "ridgecrest",
"rio del mar", "ripon",
"west whittier-los nietos", "westlake village", "westminster",
"westmont",
"whittier", "wildomar", "willowbrook", "willows", "windsor", "winter
gardens","winters", "winton", "woodcrest", "woodlake", "woodland",
"yorba linda", "yreka", "yuba city", "yucaipa", "yucca valley"
);
sub match_City {
my( $findme ) = @_;
my( $start, $end ) = ( 0, $#CA_Cities );
my( $idx );
my $cnt = 0;
# convert to lowercase
$findme =~ s/([A-Z]+)/\L$1/g;
print "\nSEARCH\n";
print "findme= " . $findme . "\n";
while ( $start != $end && $cnt < $#CA_Cities) {
#note start and end never become equal in alot of cases
$idx = int (( $end - $start ) / 2) + $start;
print "a= " . $CA_Cities[ $idx ] . " ";
if ( $CA_Cities[ $idx ] eq $findme ) {
return $findme;
} elsif ( $CA_Cities[ $idx ] gt $findme ) {
$end = $idx;
} elsif ( $CA_Cities[ $idx ] lt $findme ) {
$start = $idx;
}
print "idx= " . $idx . " start= " . $start . " end= " . $end . "
cnt= " . $cnt ."\n";
$cnt++;
}
return "not found\n";
}
## Test this
print "\n" . match_City( "ripon" );
print "\n" . match_City( "riPoN" );
print "\n" . match_City( "Ripon" );
print "\n" . match_City( "yorba linda" );
print "\n" . match_City( $CA_Cities[ int(rand $#CA_Cities) ] );
print "\n" . match_City( $CA_Cities[ int(rand $#CA_Cities) ] );
print "\n" . match_City( $CA_Cities[ int(rand $#CA_Cities) ] );
print "\n" . match_City( "Fred" );