473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How can I use bind_colums to loop through my variables and find matching values?

12
Hi guys really hoping someone can advise me with this.

I have a Perl script that connect to a PostgreSQL database and retrieves fields and rows from a database of bus timetables and bus stop information. The data I am extracting is the reference number of the bus stop, the route number this bus stop serves and the distance of a point to this bus stop. The select query returns rows like this in PostgreSQL:

stop_reference | service_id | distance
----------------+------------+------------------
6200200581 | 1 | 131
6200249240 | 2 | 148.32734070292
6200249230 | 2 | 247.695377429616
6200249250 | 2 | 394.507287638644
6200249220 | 2 | 394.81641303269

Now what I want to be able to do is to loop through each of the rows from this query in Perl, and to find where the service_id is the same. And make an IF statement that says when they are the same only return the row with the shortest distance.

This is the part of the Perl script that brings my query into Perl:

$originsql = qq{ SELECT a.stop_reference, b.service_id, distance........};
$sth = $dbh->prepare( $originsql );
$sth->execute();
$sth->bind_columns( undef, \$stops, \$service, \$distance );
while( $sth->fetch() )
{

Im hoping someone can help me to do this? If you need anymore information please let me know. Thanks
Jul 2 '07 #1
2 1279
Harch84
12
OK answer was found already thanks anyway guys! Ill post it here for anyone else who is interested:
Expand|Select|Wrap|Line Numbers
  1. my $last_service_id = -8675309;    # number it cannot be
  2.  
  3. while ($sth->fetch()) {
  4.     if ($orig_service != $last_service_id) {
  5.         print "min dist for service '$orig_service' = '$orig_distance' at stop '$orig_stops' serving route number '$orig_route_number \n <p><p/>";
  6.         $last_service_id = $orig_service;
  7.     }
  8. }
  9.  
* The rows from the database were ordered by service first in the query. Hope this helps someone who has a similar problem.
Jul 2 '07 #2
miller
1,089 Expert 1GB
Very good. However, don't forgot to include your or die statement with your execute. It's simply a good habit to have:

Expand|Select|Wrap|Line Numbers
  1. $sth->execute() or die $dbh->errstr;
  2.  
- Miller
Jul 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
4
by: david.jebo | last post by:
I have written this code and it executes well but I would like to instead of using multiple if statements write a loop that will do the exact same thing. Can anyone help me with writing a loop? ...
4
by: Tom | last post by:
Wta is the code structure to put Rst.FindNext in a loop? I'm using what I show below. It works fine but my instinct says it should be in some standard loop rather than using the GoTo structure. ...
3
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the...
0
by: Brett | last post by:
Using DOM in IE (SHDocVw), how can I loop through FORMs and access FORM elements in a specific form? For example, www.hotmail.com has about 13 forms. I believe the one displayed is dependent on...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
6
by: andyalean1 | last post by:
Hello, I am trying to match an Id number that is shared across 2 xml files.I seem to be do the right thing bu it won`t display a match.Can you please help me find my error. I have a problem matching...
12
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi all I'm creating a hashtable of a list of users in AD. The keys are UserID and Password. I want to loop through the table to compare the userID and password with values that a user has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.