Hi guys need some serious help I have a PostgreSQL database full of bus routes and bus stops. I am drawing them out using perl and manipulating the data. What I need to be able to do is to compare the variables from one query to the variables in another. Allow me to explain further. Firstly all unique routes are selected within a specific distance of my origin point.
$originsql = qq{ SELECT a.stop_reference, b.service_id, distance(Point +FromText('POINT($origin)', 27700),east_north), c.route_number FROM bu +s_stops a, service b, routes c WHERE distance(PointFromText('POINT($o +rigin)', 27700),east_north) < 200 AND a.stop_reference = b.stop_refer +ence AND b.service_id = c.service_id ORDER BY b.service_id, distance( +PointFromText('POINT($origin)', 27700),east_north)}; $sth = $dbh->prepare( $originsql ); $sth->execute(); $sth->bind_columns( undef, \$origin_stops, \$origin_service, \$origin_ +distance, \$origin_route_number); while ($sth->fetch()) { if ($orig_service != $last_service_id) { print "The bus stop located nearest to your Origin area is: " +.$origin_stops."<br />\n"; print "And the bus stop is ".$origin_distance." metres away from y +our location <br />\n"; print "This stop serves route number ".$origin_route_number." <p>< +p/>\n"; $last_orig_service_id = $origin_service; } }
The same logic is applied to the destination point with a similar query:
$destsql = qq{ SELECT a.stop_reference, b.service_id, distance(PointFr +omText('POINT($dest)', 27700),east_north), c.route_number FROM bus_st +ops a, service b, routes c WHERE distance(PointFromText('POINT($dest) +', 27700),east_north) < 200 AND a.stop_reference = b.stop_reference A +ND b.service_id = c.service_id ORDER BY b.service_id, distance(PointF +romText('POINT($dest)', 27700),east_north)}; $sth = $dbh->prepare( $destsql ); $sth->execute(); $sth->bind_columns( undef, \$destination_stops, \$destination_service, + \$destination_distance, \$destination_route_number); while ($sth->fetch()) { if ($dest_service != $last_service_id) { print "The bus stop located nearest to your Origin area is: " +.$destination_stops."<br />\n"; print "And the bus stop is ".$destination_distance." metres away f +rom your location <br />\n"; print "This stop serves route number ".$destination_route_number." + <p><p/>\n"; $last_dest_service_id = $destination_service; } }
Now what I need to be able to do is the compare the "$origin_service" to the "$destination_service". Now a simple if($origin_service == $destination_service) clause wont work as it seems to only compare the last two routes from each query whereas I need it to loop through all of the results for each and find matching pairs. One idea I had was to enter each origin and destination service numbers into two arrays and then compare them both? Or would I need some sort of double foreach statement to loop through both the origin and destination numbers finding mathcing pairs? I have tried many optins but cant seem to figure it out! Your help will be much appreciated. Thanks

In reply to Comparing two variables from different loops by Harch84

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.