Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
LightI3ulb
Oct 28, 2006

Standard pleasure model.
I'm having an incredibly strange issue. I'm grabbing info from an sql database to fill a spreadsheet with info that dates back 45 days, and I am keeping track of the number of days that are actually being filled in for purposes of getting accurate averages. This variable is incremented sequentially when it prints a value onto the spreadsheet. This is being done in one giant for loop. In the next one, I use the variable to calculate the averages, but I am running into divide by zero errors. Between the for loops, I have the variable printing out, and in the averaging loop, I have it printing if the variable is 0. This is the output from the program:

Daycount dbz: 0
Daycount dbz: 0
Daycount dbz: 0
Daycount dbz: 0
Daycount dbz: 0
12345

Technically speaking, the 12345 should be on the top, because that's the output from the print between the for loops, and the Daycount dbz's should be on the bottom, because they're literally the last line of code. Is this a scope issue? The variable is declared at the top of the subroutine.

Adbot
ADBOT LOVES YOU

LightI3ulb
Oct 28, 2006

Standard pleasure model.
I'm having a hell of a time trying to figure out why when I try to open a file, it sets the filehandle to the name of the file and not the lines inside of the file.

code:
#!/usr/bin/perl
use Data::Dumper;

my $time_stamp = "";
my $email = "";
my $phone = "";
my @lines;
my @matches;
opendir(DIRENT, "/var/local/translog/do_not_market");
my @entries = readdir(DIRENT);
foreach $name (@entries){
	if (($name ne ".") && ($name ne "..")){
		open(LOG, '<', \$name);
		@lines = <LOG>;
		foreach $line (@lines){
# 			$line = '%customer_information = ("time_stamp" =>
"2008-03-04 16:39:56","email" => "test\@aol.com","phone" => "6663332222");"';
			$time_stamp = $line;
			$email = $line;
			$phone = $line;
			$time_stamp =~ s/\D*\=\>\s\"//;
			$time_stamp =~ s/\"\,\"\D*\d*\"\)\;\"//;
			$email =~ s/\D*\=\>\s\"$time_stamp\"\,//;
			$email =~ s/\"email\"\s\=\>\s\"//;
			$email =~  s/\"\,\"\D*\d*\"\)\;\"//;
			$phone =~ s/\D*\=\>\s\"$time_stamp\"\,\"email\"\s\=\>\s\"//;
			$phone =~ s/\D*\"\,//;
			$phone =~ s/\"phone\"\s\=\>\s\"//;
			$phone =~ s/\"\)\;\"//;
			$email =~ s/\\//;

		}
	}
}
print $time_stamp . " " . $email . " " . $phone, "\n";

LightI3ulb
Oct 28, 2006

Standard pleasure model.
drat, that was it. Thanks.

As to the by reference thing, I have no idea. That's the only way I could get it to work. If I tried anything else, the file wouldn't open.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
HELP!

What the hell is going on with this?
code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @csva;
my @loga;
my $csvdir;
my $logdir;
my $csv_dir = "/home/jnooraga/csvdir/";
my $log_dir = "/home/jnooraga/csvdir/logs/";
my $out;
my $csv_fh;
my $log_fh;
my $csv;
my $log;
my $csvline;
my $logline;

opendir $csvdir, $csv_dir;
my @csvfiles = grep {!/^\./} readdir $csvdir;
closedir $csvdir;

opendir $logdir, $log_dir;
my @logfiles = grep {!/^\./} readdir $logdir;
closedir $logdir;

foreach (my $csvfile (@csvfiles)){
	next if (-d "$csv_dir$csvfile");
	open $out, ">", "$log_dir$csvfile";
	open $csv, "<", "$csv_dir$csvfile";
	$csvline = <$csv>;
	while ($csvline = <$csv>){
		chomp($csvline);
		@csva = split(',', $csvline);
		foreach (my $logfile (@logfiles)){
			next if (-d "$log_dir$logfile");
			open $log, "<", "$log_dir$logfile";
			my $logline = <$log>;
			while ($logline = <$log>) {
# 				$logline =~ s/\"*//g;
				@loga = split(',', $logline);
				if ($loga[0] eq $csva[2]){
					for (my $j=0;$j<scalar(@csva);$j++){
						print $out $csva[$j].",";
					}
					print $out "\"".$loga[13]."\"\n";
				}
			}
			close $log;
		}
	}
	close $csv;
	close $out;
}
Why is it giving me syntax errors?

LightI3ulb fucked around with this message at 13:58 on May 14, 2008

LightI3ulb
Oct 28, 2006

Standard pleasure model.

fansipans posted:

Always, always, always include these two lines:

code:
use strict;
use warnings;
Including those two lines doubles the error messages your script outputs, and will probably help you quickly identify where the problem is.

Fixed. Still clueless.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
syntax error at ipcsv.pl line 27, near "$csvfile ("
syntax error at ipcsv.pl line 30, near ""$csv_dir$csvfile";"
syntax error at ipcsv.pl line 35, near "$logfile ("
syntax error at ipcsv.pl line 38, near "<$log>;"
syntax error at ipcsv.pl line 51, near "}"
syntax error at ipcsv.pl line 54, near "}"
Execution of ipcsv.pl aborted due to compilation errors.

LightI3ulb
Oct 28, 2006

Standard pleasure model.

Triple Tech posted:

It's like we need to hold workshops on how to write well-formatted Perl... Less is more. You need less parenthesis, shorter synonyms, and more strategic use of whitespace. Also, a two-space tab indent.

Did you know you're popping off the first line of the filehandle before the while loop?

Yes, the first line of the file is the headers, which I don't want.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
Wow.

:suicide:

LightI3ulb
Oct 28, 2006

Standard pleasure model.
Does anyone know of any modules that would allow me to receive mail and work with the attachments? The ones I've found on google are typically just for sending.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
Does anyone know of any modules that would allow me to receive mail and work with the attachments? The ones I've found on google are typically just for sending.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
I want to be able to access my mailbox, download the email message with the attachment (which I think will be CSV), and then use the CSV like a normal file.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
I'm really confused as to why my script keeps giving me the errors

DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at ./tmcseed.pl line 24.
DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at ./tmcseed.pl line 38.

on my code
code:
#!/usr/bin/perl -w
use strict;

use DBI;
my $dbh = DBI->connect( 'DBI:mysql:database=CONSUMERS;host=localhost;
port=3306;', '<user>', '<password>', undef);
my $sth0;
my $sth1;
my $sth2;

for(my $i=0;$i<1;$i++){
   $sth0 = $dbh->prepare(qq[select EMAIL, HOME_PHONE from RAW_CONSUMERS
 where date(creation_date)=date_sub(curdate(), INTERVAL '$i' DAY)
 and CONTRACT_ID='46']);
   $sth0->execute or die $dbh->errstr;
   my $num_rows = $sth0->rows;
   $sth1 = $dbh->prepare(qq[SELECT PHONE FROM PHONE_DETAILS
 where date(SCRUB_DATE)=date_sub(curdate(), INTERVAL '$i' DAY) 
and (NATIONAL_DNC='1' or STATE_DNC='1' or WIRELESS_IN_BLOCKED_STATE='1')
and VALID='1']);
   $sth1->execute or die $dbh->errstr;
   $sth2 = $dbh->prepare(qq[select EMAIL from RAW_CONSUMERS where
 CONTRACT_ID <> 46 and date(CREATION_DATE)=
date_sub(curdate(), INTERVAL '$i' DAY)]);
   $sth2->execute or die $dbh->errstr;
   my $dnccount = 0;
   my $dupcount = 0;
   while(my $data = $sth0->fetchrow_hashref()){

      while (my $dncdata = $sth1->fetchrow_hashref()){
         my $phone = "";
         my $home_phone = "";
         if (defined $dncdata->{'PHONE'}){
            $phone = $dncdata->{'PHONE'};
         }
         if (defined $data->{'HOME_PHONE'}){
            $home_phone = $data->{'HOME_PHONE'};
         }
         if ($phone eq $home_phone){
            $dnccount++;
         }
      }

      while (my $dupdata = $sth2->fetchrow_hashref()){
         my $email = "";
         my $email2 = "";
         if (defined $dupdata->{'EMAIL'}){
            $email = $dupdata->{'EMAIL'};
         }
         if (defined $data->{'EMAIL'}){
            $email2 = $data->{'EMAIL'};
         }
         if ($email eq $email2){
            $dupcount++;
         }
      }
   }
   print $num_rows." ".$dnccount." ".$dupcount;
}
The lines in question are the nested while loops where it's trying to fetch from $sth1 and $sth2. The output shows no errors from the executes, so I'm really not sure where this is going wrong.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
What's headache-inducing?

LightI3ulb
Oct 28, 2006

Standard pleasure model.

magimix posted:

I might be flying off in totally the wrong direction, but can MySQL actually handle multiple active statements on a single database handle?

I've always found I couldn't execute a second statement on a DB handle without first fetching or discarding the result-set of the previously executed statement.

Yeah, I've done it a ton of times.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
See, that's the strangest part. I checked the return values from the executes, and it gave me the number of rows it returned just like it was actually working, but still giving me execute errors.

Adbot
ADBOT LOVES YOU

LightI3ulb
Oct 28, 2006

Standard pleasure model.
Is the result set in the stored in the dbh or the sth? If it's dbh, then I understand why I had that problem. Regardless, I realised I was able to do that entire comparison with mysql joins. Thanks for the help.

  • Locked thread