Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can I get a second pair of eyes on this sort ...

Reply
Thread Tools

Can I get a second pair of eyes on this sort ...

 
 
MW
Guest
Posts: n/a
 
      08-27-2003
Found this great store locator script, tweaked it - works great -
except:

Having trouble getting it to sort by distance ($dist). I'm sure I'm
missing something stupid and thought I could get a second pair of eyes
on it to tell me what I'm missing.

#!/usr/bin/perl -w

# ------------------------------------
# Store Locator
# ------------------------------------
print "Content-Type: text/html\n\n" ;

# Retrieve zipcode from form variable.
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
foreach (split(/&/, $input))
{
($NAME, $VALUE) = split(/=/, $_);
$NAME =~ s/\+/ /g;
$NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
$VALUE =~ s/\+/ /g;
$VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
}

$zipcode = $VALUE;

# Open DBI connection with MySQL.
use DBI;
$dbh = DBI->connect("DBI:mysql:mydb;", "myid", "mypwd")
or die "Can't connect: $_";

# Set PI and radius in miles of results to show.
$PI = 3.1415926;
$radius = 50;

# Prepare SQL.
$sth = $dbh->prepare("select city, state, latitude, longitude " .
"from zipcodes where zipcode = '$zipcode'")
or die "Can't prepare zipcode: $dbh->errstr\n";

# Execute SQL.
$rv = $sth->execute
or die "Can't execute the query: $sth->errstr\n";

($city, $state, $latitude, $longitude) = $sth->fetchrow_array;

$sth = $dbh->prepare("select store_name, store_phone, store_address1,
store_city, store_state, store_zipcode," .
" latitude, longitude from stores, zipcodes ".
"where stores.store_zipcode = zipcodes.zipcode");

$rv = $sth->execute
or die "Can't execute the query: $sth->errstr\n";

undef %store_distances;

while (($name, $s_phone, $s_address, $s_city, $s_state, $s_zipcode,
$s_lat, $s_long) = $sth->fetchrow_array)
{

# Calculate distance.
$prodsin = sin($latitude) * sin($s_lat);
$prodcos = cos($latitude) * cos($s_lat);
$deltalong = cos(abs($s_long-$longitude));
$dist = &acos($prodsin + ($prodcos * $deltalong));
$dist = $dist * (180.0/$PI) * 69.0;

$store_distances{"$name|$s_address|$s_city|$s_stat e|$s_zipcode|$s_phone"}
= $dist;

}

# Sort the array.
# @ordered = sort {$store_distances{$a} > $store_distances{$b}}
keys(%store_distances);
@ordered = sort keys(%store_distances);

# Print page header.
print "<html>" .
"<head>" .
"<title>" ........



# Print each record.
foreach $item (@ordered)
{
if ($store_distances{$item} <= $radius)
{



($s_name, $s_address, $s_city, $s_state, $s_zipcode, $s_phone) =
split(/\|/, $item);


print "<tr>" .
"<td>".
$s_name .
"</td>" .
"<td>" .
$s_address . "<br />" .
"$s_city, $s_state $s_zipcode <br />" .
"<a href=\"http://maps.yahoo.com/py/maps.py?Pyt=Tmap&addr=" .
$s_address .
"&csz=" .
$s_zipcode .
"&Get%A0Map=Get+Map\" target=\"_blank\" title=\"Show me a map of
this address.\">Map</a>" .
"</td>" .
"<td nowrap>" .
$s_phone .
"</td>" .
"<td align=\"right\">" .
sprintf("%d", $store_distances{$item}) .
"</td>" .
"</tr>";
}
}

# Print page footer.
print "<tr><td colspan=\"4\" align=\"center\">&nbsp;</td></tr>" .
"</body>" .
"</html>";


sub acos
{
($val) = @_;
return(atan2(sqrt(1-($val * $val)),$val));
}

sub asin
{
($val) = @_;
return(atan2($val, sqrt(1-($val * $val))));
}
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      08-27-2003
>>>>> "M" == MW <> writes:


M> #!/usr/bin/perl -w

no strict, no warnings.

M> # Retrieve zipcode from form variable.
M> read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
M> foreach (split(/&/, $input))

broken!!
M> {
M> ($NAME, $VALUE) = split(/=/, $_);
M> $NAME =~ s/\+/ /g;
M> $NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
M> $VALUE =~ s/\+/ /g;
M> $VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;

and what if there is more than one param set? this is the worst cgi
parser i have ever seen. matt's at least (brokenly) handled multiple
values.

M> }

i refuse to look at any more code until you fix that heap of trash. use
cgi.pm


<snip of bad script kiddie code>

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      08-27-2003
On 26 Aug 2003 20:54:44 -0700, MW <> wrote:
> Found this great store locator script, tweaked it - works great -
> except:
>
> Having trouble getting it to sort by distance ($dist). I'm sure I'm
> missing something stupid and thought I could get a second pair of eyes
> on it to tell me what I'm missing.


Those two paragraphs contradict each other. Clearly if it doesn't "work great".

Ask the vendor of the script to fix it for you.

[ Snip cargo cult garbage broken perl code]

--
Sam Holden

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-27-2003
MW <> wrote:

> Found this great store locator script, tweaked it - works great -



No it doesn't. You just haven't seen any of its failure modes yet.


> Having trouble getting it to sort by distance ($dist). I'm sure I'm
> missing something stupid



Like asking a FAQ?


> on it to tell me what I'm missing.



perldoc -q sort

How do I sort a hash (optionally by value instead of key)?


> #!/usr/bin/perl -w



Ask for all the help you can get:

use strict;
use warnings;


> # Retrieve zipcode from form variable.
> read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
> foreach (split(/&/, $input))



Use the CGI.pm module for parsing form values.


> $store_distances{"$name|$s_address|$s_city|$s_stat e|$s_zipcode|$s_phone"}
>= $dist;


> # Sort the array.
> # @ordered = sort {$store_distances{$a} > $store_distances{$b}}

^^^
^^^

That was pretty close:

@ordered = sort {$store_distances{$a} <=> $store_distances{$b}} keys...


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      08-28-2003

"Lori Fleetwood" <> wrote in message
news: om...
>
> You're such a ****ing douchebag.


Holy 1980s comebacks Batman! That just cracked me up... : )

I have to say, however, that your little invective is the more pointless.
The code posted *is* crap, it's doubtful the OP has any clue how it works,
and Uri was somewhat restrained (for Uri). Rather than call people names,
you would have done more of a service to the poster by responding to his/her
question. If you had, you would have come to the same conclusion that
everyone else has.

Matt


 
Reply With Quote
 
Lori Fleetwood
Guest
Posts: n/a
 
      08-28-2003
Matt Garrish wrote:
> "Lori Fleetwood" <> wrote in message
> news: om...
>
>>You're such a ****ing douchebag.

>
>
> Holy 1980s comebacks Batman! That just cracked me up... : )


Dont'cha watch'da Sopranos? Betcha Uri does, wid'this Lawn Guyland
attitude.

> I have to say, however, that your little invective is the more pointless.
> The code posted *is* crap, it's doubtful the OP has any clue how it works,
> and Uri was somewhat restrained (for Uri). Rather than call people names,
> you would have done more of a service to the poster by responding to his/her
> question. If you had, you would have come to the same conclusion that
> everyone else has.


I'll go further. It's ironic that the author should use the DBI but not
CGI.pm.

Another thing I might point out to the OP from a user perspective is
that I absolutely HATE store locators that require the user know the zip
code. Sometimes I'm travelling, or plan on travelling, and don't know
the zip code. These things should accept city/state, at least for the US.


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-28-2003
>>>>> "LF" == Lori Fleetwood <> writes:

LF> Uri Guttman <> wrote in message news:<>...
>>
>> <snip of bad script kiddie code>
>>
>> uri


LF> You're such a ****ing douchebag. Just don't reply. This is why my
LF> company does google and usenet archive searches on potential
LF> consultants: as a tool in weeding out the ****ing assholes. We'd
LF> rather have somebody with two thirds the talent and one third -- or
LF> less -- the attitude.

and you are a dumbshit who uses broken code and doesn't hire smart
people. nyah nyah nyah!!!

and i wouldn't work for you if you could afford me.

nyah nyah nyah!!!

and your mother has a moustache!!

nyah nyah nyah!!!

and i have more skill than you imagine, especially insulting twits like
you. nyah nyah nyah!!!!

hope you feel better soon (especially after you wake up from the crash
your broken crapola of a script brings to you).

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-28-2003
>>>>> "MG" == Matt Garrish <> writes:

MG> "Lori Fleetwood" <> wrote in message
MG> news: om...
>>
>> You're such a ****ing douchebag.


MG> Holy 1980s comebacks Batman! That just cracked me up... : )

that was funny? lori can't program her way out of the batcave with
thirty nerds in her utility belt.


MG> I have to say, however, that your little invective is the more
MG> pointless. The code posted *is* crap, it's doubtful the OP has
MG> any clue how it works, and Uri was somewhat restrained (for
MG> Uri). Rather than call people names, you would have done more of a
MG> service to the poster by responding to his/her question. If you
MG> had, you would have come to the same conclusion that everyone else
MG> has.

i am glad you agreed with the code quality. one point of karma in your
favor. but lori needs a cranial enema to clear out her attitude about
attitude. bad code deserves slamming and she doesn't get that.

ok, lori this is for you.

<sarcasm alert! sarcasm alert! sarcasm alert!>

i wish i were as nice and smart as you. i see you help people here all
the time and you fix all their bugs no matter where the code came
from. too bad i was raised in the wrong part of the world and not by
your sweet parents who bestowed their special skills on their little
baby. you handle so many perl issues with such elan and flair, it makes
me ashamed to be just a perl guru. what can i ever do to become as
useful to the perl community as you?

<end of sarcasm alert! if this had been an honest response to lori it
would have been done from beyond the grave. now back to your regular
perl flamage>

lori, too bad you don't know perl. at least moronzilla tries to leanr
new stuff (about once a year she tackles regexes and slided down the
slope back to her beloved subtr and index).

uri (having some fun tonight!)

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      08-28-2003

"Lori Fleetwood" <> wrote in message
news:atd3b.2095$.. .
> Matt Garrish wrote:
> >
> > Holy 1980s comebacks Batman! That just cracked me up... : )

>
> Dont'cha watch'da Sopranos? Betcha Uri does, wid'this Lawn Guyland
> attitude.
>


Can't say I watch much on TV but movies. The last conversation I heard with
douchebag in it went something like this:

"You're a douchebag!"
"No, *you're* a douchebag!"
"No, *YOU*'re a douchebag!"

One of the many reasons I'm glad grade school is a distant memory...

>
> Another thing I might point out to the OP from a user perspective is
> that I absolutely HATE store locators that require the user know the zip
> code. Sometimes I'm travelling, or plan on travelling, and don't know
> the zip code. These things should accept city/state, at least for the US.
>


Note to self: always be extra accomodating to Americans when building Web
sites... ; )

Matt


 
Reply With Quote
 
MW
Guest
Posts: n/a
 
      08-29-2003
>
> That was pretty close:
>
> @ordered = sort {$store_distances{$a} <=> $store_distances{$b}} keys...




Thanks, Tad - that did the trick! I knew it was going to end up being
a less-than sign somewhere.

Thanks for the tip on using CGI.pm as well. I can't believe how easy
that was to switch to and use instead.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
J2ee: need another pair of eyes. DaLoverhino Java 1 11-22-2005 07:48 PM
Object expected; can't find it. Need another pair of eyes. Laiverd.COM Javascript 9 08-05-2004 06:46 PM
Re: need second pair of eyes: databinder.eval problem Phil Winstanley [Microsoft MVP ASP.NET] ASP .Net 3 06-21-2004 06:24 PM
need second pair of eyes: databinder.eval problem darrel ASP .Net 0 06-21-2004 03:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57