The DBI code is also fairly simple...You have to set the field separator to be tab instead of the comma default. The easiest way is to treat each file as a separate table, below the file "patients" is the table with the data. There is a fair amount to learn about SQL and the DBI if these are completely new topics. This approach is the most extensible but it is also the most work.
#!/usr/bin/perl -w
use strict;
use DBI;
# csv_sep_char=\t means tab separated, default is a comma of course
my $dbh = DBI->connect("DBI:CSV:csv_sep_char=\t;RaiseError=1")
or die "Cannot connect: " . $DBI::errstr;
my $sth = $dbh->prepare("SELECT * FROM patients
WHERE bmi>24 AND bp>135 AND age<30");
$sth->execute();
while (my $row = $sth->fetchrow_arrayref())
{
print "@$row\n";
}
__END__
Prints:
Jane 50 23 200
Joe 30 22 140
File:patients contains tab separation in real file,
my editor converts them to spaces here:
name bmi age bp
Jane 50 23 200
Bob 25 55 120
Norm 28 30 136
Joe 30 22 140
Ben 24 85 110
Now of course, if this is a "one off" thing, Excel is capable of importing this tab delimited file and the query tool would get you a result set too.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.