Hello monks...
Update: merlyn is right about the missing row problem. I should have had that second cup of coffee before I posted. I hope the missing column problem is that easy to solve.
Update:changed typo: "=" to "==" in if statement.
DBD::CSV has mostly done the right thing for me, but I ran into a couple of issues with a particular set of data. I have a file and I want to print the results of fetchrow_array if a particular row matches any element of a different array, @chk. Here's some test input data, a file called "DUDS.csv":
ADT,,111111111,,*SOH,5.00,,,,,,,,#
ADS,,222222222,,*SOH,1312.00,,,,,,,,#
ADS,,111111111,,*FWT,14.00,,,,,,,,#
ADS,,222222222,,*UNLF,99.75,,,,,,,,#
Here's a runnable test script that shows my problems:
use warnings;
use strict;
use DBI;
open (OUT, ">file.txt");
my @chk = (111111111,222222222);
my $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\,});
$dbh->{'csv_tables'}->{'comp'} = { 'file' => 'DUDS.csv'};
my $sql;
my @row ;
my $sth = $dbh->prepare("SELECT * from comp");
$sth->execute();
while (my @row = $sth->fetchrow_array) {
foreach my $chk(@chk) {
if ( $chk == $row[2]) {
print OUT "@row\n";
print OUT "$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$ro
+w[7]\n\n";
last;
}
}
}
The output from this script is below. I'm confused because: I think the first row of the input file should be represented; and because the numeric value in the sixth column has disappeared. I don't think this is right, but I don't see anything wrong with my code (yet!)
ADS 222222222 *SOH #
ADS,,222222222,,*SOH,,,
ADS 111111111 *FWT #
ADS,,111111111,,*FWT,,,
ADS 222222222 *UNLF #
ADS,,222222222,,*UNLF,,,
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.