J
jim.goodman
I have a script that reads a bunch of files and extracts some data....
I then want to take a piece of that data and do a "select" query on the
Db, get a particular result and then use that for a second sql
statement, an insert/update....
here is my code and data.....
#!/usr/bin/perl
use strict; use warnings;
use DBI;
my $dir="/Users//Wine/files/";
opendir(DIRECTORY, $dir) || die("Cannot open directory");
my @thefiles= readdir(DIRECTORY);
closedir(DIRECTORY);
my $dsn = "DBI:mysql:database=wine;host=domain.com";
my $dbh = DBI->connect($dsn, "user", "password") || die ("Could not
open DB!");
my $n=0, my $m=0;
foreach my $file (@thefiles) {
my (@lines);
unless ( $file =~ /^\./ ) {
$n++;
open FILE, "$dir/$file" or die "Can't open $file : $!";
while( <FILE> ) {
s/\t//; # ignore tabs by erasing them
next if /^(\s)*$/; # skip blank lines
chomp; # remove trailing newline characters
push @lines, $_; # push the data line onto the array
}
close FILE;
$file=$1 if $file =~ /(.*?)\.html/i;
my $chunk = "@lines";
$chunk =~ s/\s\s\s+//g if $chunk;
$chunk=$1 if $chunk =~ /<\!\-\-.Hitbox.variables:.*?<TR
HEIGHT=15><TD><\/TR>(.*?)<\!\-\-.END.PAGE.BODY.\-\->/;
my @data = ( split /<TR HEIGHT=15><TD><\/TR>/, $chunk );
foreach my $data (@data) {
$m++ if $data;
my $winedata=$1 if ($data=~ /<TR>\s*<td
colspan=\"2\">.*?<b>(.*?<)\/B><\/FONT>\s*<\/TD>/isg);
my ($winery, $wine, $vintage)=($1, $2, $3) if
($winedata=~ /(.*?)\ \;(.*?)\ \;(.*?)</is);
my $sql = qq{
SELECT winery_id
FROM winery
WHERE winery='$winery'
};
my $sth = $dbh->prepare($sql);
$sth->execute() || die ("Query failed");
my @winery_id = $sth->fetchrow_array();
print "$winery\t$winery_id[0]\n";
my $rows = $dbh->do("UPDATE wine SET
winery_id='$winery_id[0]' WHERE winery='$winery' LIMIT 1");
}
$dbh->disconnect ();
}
}
__DATA__
<!-- Hitbox variables:
blank; blank; blank-->
<TABLE BORDER="0" WIDTH="100%" cellspacing="0" cellpadding="3">
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>AQUILA Merlot Margaret River
1999</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>83</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $16 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black"
SIZE="2">Australia<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black"
SIZE="2">Australia<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Web Only
(2002)<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">Supple, graceful style offers pleasant berry and herb flavors,
finishing with a tang of roasted red pepper. Drink now. 2,000 cases
made. <I>(HS)</I><BR>
</TD>
</TR>
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>BODEGAS 1890 Tempranillo Jumilla Don
Luciano 1998</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>73</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $5 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Dec 15,
1999<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">This light red has a core of sweet cherry, but it tastes
candied and turns bitter on the finish. 50,000 cases made.
<I>(TM)</I><BR>
</TD>
</TR>
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>BODEGAS 1890 Jumilla Castillo San
Simón 1997</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>79</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $5 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Oct 31,
1998<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">This light, simple red offers pretty strawberry flavors, with
hints of spice. It has very light tannins and finishes short and clean.
<I>(TM)</I><BR>
</TD>
</TR>
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>ABARBANEL Beaujolais-Villages Mevushal Château de la
Salle 2000</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>82</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $9 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">France<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black"
SIZE="2">Beaujolais<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Sep 15,
2001<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">Kosher. Sweet strawberry and red cherry flavors, with a dash
of spice on the finish. Drink now. 1,000 cases made. <I>(JM)</I><BR>
</TD>
</TR>
</TABLE>
<BR><BR>
<!-- END PAGE BODY -->
__END DATA__
when i execute the code... i get the following error...
Use of uninitialized value in concatenation (.) or string at wine_data
v1.2.pl line 43.
when the data that i want is.... i should have the variable $winery and
the variable $winery_id which i have retrieved from the Db.... if i
were to print them (which i do in the code so that i can watch the
output as there is an insert to the Db....) it would look like this....
AQUILA 26
BODEGAS 1890 14
etc....
i know that the code may or may not be pretty, i am learning.... but
where i am having problems is the code towards the end, post the SQL
statement, where i am trying to get a value from the Db and store it
then use it as a variable, along with other variables that i have
gotten from extracting text from the file.... thanks for the help and
please let me know if i can answer any other questions so as to get a
more complete/full answer.
I then want to take a piece of that data and do a "select" query on the
Db, get a particular result and then use that for a second sql
statement, an insert/update....
here is my code and data.....
#!/usr/bin/perl
use strict; use warnings;
use DBI;
my $dir="/Users//Wine/files/";
opendir(DIRECTORY, $dir) || die("Cannot open directory");
my @thefiles= readdir(DIRECTORY);
closedir(DIRECTORY);
my $dsn = "DBI:mysql:database=wine;host=domain.com";
my $dbh = DBI->connect($dsn, "user", "password") || die ("Could not
open DB!");
my $n=0, my $m=0;
foreach my $file (@thefiles) {
my (@lines);
unless ( $file =~ /^\./ ) {
$n++;
open FILE, "$dir/$file" or die "Can't open $file : $!";
while( <FILE> ) {
s/\t//; # ignore tabs by erasing them
next if /^(\s)*$/; # skip blank lines
chomp; # remove trailing newline characters
push @lines, $_; # push the data line onto the array
}
close FILE;
$file=$1 if $file =~ /(.*?)\.html/i;
my $chunk = "@lines";
$chunk =~ s/\s\s\s+//g if $chunk;
$chunk=$1 if $chunk =~ /<\!\-\-.Hitbox.variables:.*?<TR
HEIGHT=15><TD><\/TR>(.*?)<\!\-\-.END.PAGE.BODY.\-\->/;
my @data = ( split /<TR HEIGHT=15><TD><\/TR>/, $chunk );
foreach my $data (@data) {
$m++ if $data;
my $winedata=$1 if ($data=~ /<TR>\s*<td
colspan=\"2\">.*?<b>(.*?<)\/B><\/FONT>\s*<\/TD>/isg);
my ($winery, $wine, $vintage)=($1, $2, $3) if
($winedata=~ /(.*?)\ \;(.*?)\ \;(.*?)</is);
my $sql = qq{
SELECT winery_id
FROM winery
WHERE winery='$winery'
};
my $sth = $dbh->prepare($sql);
$sth->execute() || die ("Query failed");
my @winery_id = $sth->fetchrow_array();
print "$winery\t$winery_id[0]\n";
my $rows = $dbh->do("UPDATE wine SET
winery_id='$winery_id[0]' WHERE winery='$winery' LIMIT 1");
}
$dbh->disconnect ();
}
}
__DATA__
<!-- Hitbox variables:
blank; blank; blank-->
<TABLE BORDER="0" WIDTH="100%" cellspacing="0" cellpadding="3">
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>AQUILA Merlot Margaret River
1999</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>83</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $16 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black"
SIZE="2">Australia<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black"
SIZE="2">Australia<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Web Only
(2002)<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">Supple, graceful style offers pleasant berry and herb flavors,
finishing with a tang of roasted red pepper. Drink now. 2,000 cases
made. <I>(HS)</I><BR>
</TD>
</TR>
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>BODEGAS 1890 Tempranillo Jumilla Don
Luciano 1998</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>73</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $5 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Dec 15,
1999<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">This light red has a core of sweet cherry, but it tastes
candied and turns bitter on the finish. 50,000 cases made.
<I>(TM)</I><BR>
</TD>
</TR>
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>BODEGAS 1890 Jumilla Castillo San
Simón 1997</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>79</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $5 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Spain<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Oct 31,
1998<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">This light, simple red offers pretty strawberry flavors, with
hints of spice. It has very light tannins and finishes short and clean.
<I>(TM)</I><BR>
</TD>
</TR>
<TR HEIGHT=15><TD></TR>
<TR><td colspan="2"><FONT FACE="helvetica, arial" color="black"
SIZE="2"> <b>ABARBANEL Beaujolais-Villages Mevushal Château de la
Salle 2000</B></FONT></TD>
</TR>
<TR><TD><FONT FACE="helvetica, arial" SIZE="2">Score:
<B>82</B></FONT></TD>
<TD valign="top" WIDTH="25%" ROWSPAN=2>
<table border="0" cellspacing="0" cellpadding="0">
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Price:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2"> $9 </FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Country:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">France<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Region:</FONT> <FONT
FACE="helvetica, arial" color="black"
SIZE="2">Beaujolais<BR></FONT></TD>
</TR>
<TR>
<TD><FONT FACE="helvetica, arial" SIZE="2">Issue:</FONT> <FONT
FACE="helvetica, arial" color="black" SIZE="2">Sep 15,
2001<BR></FONT></TD>
</TR></table>
</TD>
</TR>
<TR>
<TD valign="top" WIDTH="75%"><FONT FACE="helvetica, arial"
SIZE="2">Kosher. Sweet strawberry and red cherry flavors, with a dash
of spice on the finish. Drink now. 1,000 cases made. <I>(JM)</I><BR>
</TD>
</TR>
</TABLE>
<BR><BR>
<!-- END PAGE BODY -->
__END DATA__
when i execute the code... i get the following error...
Use of uninitialized value in concatenation (.) or string at wine_data
v1.2.pl line 43.
when the data that i want is.... i should have the variable $winery and
the variable $winery_id which i have retrieved from the Db.... if i
were to print them (which i do in the code so that i can watch the
output as there is an insert to the Db....) it would look like this....
AQUILA 26
BODEGAS 1890 14
etc....
i know that the code may or may not be pretty, i am learning.... but
where i am having problems is the code towards the end, post the SQL
statement, where i am trying to get a value from the Db and store it
then use it as a variable, along with other variables that i have
gotten from extracting text from the file.... thanks for the help and
please let me know if i can answer any other questions so as to get a
more complete/full answer.