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":



  • 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.