Hi I have a script that draws two queries from a PostgreSQL database full of bus timetable data and then compares them for similar results in one column. The advice that I was given before was to use one ash and one array to fetermine which values are the same in either query. This ocde looks something like this:
my %ids; $origsql = qq{ SELECT route_number, stop_reference, depart_time FROM r +outes WHERE distance < 200...........}; #all SQL code not included he +re $sth = $dbh->prepare( $origsql ); $sth->execute(); $sth->bind_columns( undef, \$orig_service_id, \$orig_stop, $depart_ori +g); while ($sth->fetch()) { push(@orig_service_ids, $orig_service_id); } for my $id (@orig_service_ids) { $ids{$id} = 1; } # ... $destsql = qq{ SELECT route_number, stop_reference, depart_time FROM r +outes WHERE distance < 200...........}; #all SQL code not included he +re $sth = $dbh->prepare( $destsql ); $sth->execute(); $sth->bind_columns( undef, \$dest_service_id, \$dest_stop, $depart_des +t); while ($sth->fetch()) { push(@dest_service_ids, $dest_service_id); } for my $id (@dest_service_ids) { if (exists $ids{$id}) { print "ID $id exists in both origin and destination set\n"; } }
Now this works excellently as it tells me which service from my origin matches in my destination. However how can I also gain access to the associated data with each row in the query? For example, if I find that both queries have the same service, how can I then find the associate stops from the query relating to that service for each my origin and my destination? Do I have to make an array for each other variable in the queries too or a hash for each? Im not really an expert on hashes so any help and advice would be more than welcome. Thanks

In reply to Keeping associated data after entering into a hash 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.