SPUG: SPUG meetings -- a least path analysis

William Julien moonbeam at catmanor.com
Fri Oct 15 20:45:38 CDT 1999


>>
>Hmmm... We could do that in perl! First use the DBI module to read
>a spug database of all it's members and for each address form a maps.yahoo.com
>"Get Directions" html query. Then we use the LWP module to GET each of these
>pages and parse the html source with a regex looking for /\d+.\.?\d+ miles/
>and /\d+ mins/. (pardon if my regex is not quite right, I am regex challanged)
>

Ok guys... Here is a start. First, the input is a simple file with
the data format ...

name|address|city-state-zip|dest_name|dest_address|dest_city-state-zip

eg:

-->cat address.txt
William Julien|213 222nd Ave. NE|Redmond, WA 98053|Fred Hutchinson|1100 Fairview Ave. N|Seattle, WA 98109
Parent's Place|110 128th Ave. NE|Bellevue, WA 98004|Fred Hutchinson|1100 Fairview Ave. N|Seattle, WA 98109
Pacific Inn Pub|3501 Stone Way North|Seattle, Wa. 98103|Fred Hutchinson|1100 Fairview Ave. N|Seattle, WA 98109


The program...

#!/usr/local/bin/perl -w
#
use LWP::Simple;
#
# loop over each address
#
while (<>) {
    chomp;
    ( $name, $addr, $csz, $tname, $taddr, $tcsz ) = split /\|/;
#
# setup url
#
    $url = "http://maps.yahoo.com/py/ddResults.py?Pyt=Tmap&doit=1&newname=";
    $url .= "&newdesc=&newaddr=$addr&newcsz=$csz&newtcsz=$tcsz&";
    $url .= "Get Directions=Get+Directions";
    $url =~ s/\ /+/g;
#
# fetch html data
#
    $_ = get "$url";
#
# look for miles in the html text
#
    undef $miles;
    if ( /\d+.\.?\d+ miles/ ) {
        $miles = $&;
    }
#
# look for minutes
#
    undef $minutes;
    if ( /\d+ mins/) {
        $minutes = $&;
    }
#
# collect the results
#
    if ( defined $miles and defined $minutes ) {
        push @data, "$name\t\t$tname"; 
        push @data, "$addr\t$taddr"; 
        push @data, "$csz\t$tcsz\t$miles $minutes\n"; 
    }
} # end while
#
# print a header
#
print "From"," "x20,"To"," "x22,"Distance","\n";
print "-"x18, " "x6,"-"x18," "x6,"-"x18,"\n";
#
# print out the results
#
foreach (@data) {
    print "$_\n";
}

The output....

-->yahoo_maps.plx address.txt
>From                    To                      Distance
------------------      ------------------      ------------------
William Julien          Fred Hutchinson
213 222nd Ave. NE       1100 Fairview Ave. N
Redmond, WA 98053       Seattle, WA 98109       21.1 miles 41 mins

Parent's Place          Fred Hutchinson
110 128th Ave. NE       1100 Fairview Ave. N
Bellevue, WA 98004      Seattle, WA 98109       12.0 miles 26 mins

Pacific Inn Pub         Fred Hutchinson
3501 Stone Way North    1100 Fairview Ave. N
Seattle, Wa. 98103      Seattle, WA 98109       2.3 miles 6 mins

Gee... I love perl. Now, all we need is an address list and I can
calculate a "distance quotient" for each of the proposed new SPUG locations.

Hey Tim! This would make a good class assignment!

        _,'|            _.-''``-...___..--';
       /, \'.      _..-' ,      ,--...--'''
      < \   .`--'''      `     /| William Julien moonbeam at catmanor.com
       `-,;'              ;   ; ; http://www.catmanor.com/moonbeam/
 __...--''     __...--_..'  .;.'  vi is my shepherd; i shall not font.
(,__....----'''      (,..--''     
perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);'


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list