Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi there
I've written the following perl subroutine that takes a text file, parses it, and then uses it to get data out of a database using an SQL query. It then prints off certain results from that SQL query depending on a set of criteria.
It works fine, but in the interest of improving my perl I was wondering if anyone would like to comment on this and suggest other/better ways of doing the same thing.
my $inputfile = "file.txt"; my $dbh = DBI->connect("dbi:Pg:dbname=db;host=dbhost", "user", "passwd", {AutoCommit => 1}); unless (open(INPUT, "$inputfile")) { print "ERROR: Can't open file for reading : $!\n"; } while(<INPUT>) { # splitting on tab character my @data = split("\t", $_); # assigning varables my $did1 = $data[0]; my $did2 = $data[1]; my $score1 = $data[2]; my $score2 = $data[3]; if(($score1 >=70) && ($score2 >=60)) { my $sth1 = $dbh->prepare("select d_id, c_id from d where d_id += '$did1';"); $sth1->execute(); my $sth2 = $dbh->prepare("select d_id, c_id from d where d_id += '$did2';"); $sth2->execute(); my $array_ref1 = $sth1->fetchall_arrayref(); my $array_ref2 = $sth2->fetchall_arrayref(); foreach my $row1(@$array_ref1) { my ($did1, $cid1) = @$row1; foreach my $row2(@$array_ref2) { my ($did2, $cid2) = @$row2; if($cid2 != $cid1) { print "$did1, $did2, $cid1, $cid2\n"; } } } } } close(INPUT); }
The text file looks like this
A34253 S43154 70 67 C31243 C31243 70 73 Y54231 W65313 70 71 U65242 B65231 70 70 Z23154 Z23154 70 65
It prints out the results of the SQL query only if score1 is at least 70, score2 is as least 60 and the 'cid1' for 'did1' is not the same as 'cid2' for 'did2' (the cid codes are retrieved from the SQL query).
Thanks a lot.

2006-06-01 Retitled by planetscape, as per Monastery guidelines

( keep:0 edit:28 reap:0 )

Original title: 'Feedback Appreciated'


In reply to Feedback Appreciated on text-parsing, SQL querying subroutine by Angharad

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found