I am trying to pull 1 week data from sql server, then trying to write the data into files with separate names. Following codes does not work, and I cannot find a way to create file handlers with a variable.
Another question is that if the size of sql data is huge (1-2 gigabyte per day, and the entire table is over 1 year period), then is this monthly fetching method is faster than daily fetching?
my $sqlGetEid = "select trunc(datetime, 'DD') truncdate, x, y from z
where DATETIME between TO_DATE('$dt1','YYYY-MM-DD') and TO_DATE('$dt1','YYYY-MM-DD')+7";
for ($day=1; $day<=7; $day++){
open('dayfile_$day', "> ./temp/Wireless_$day.csv");
}
while ( my ($tday,$x,$y) = $curSqlEid->fetchrow_array() )
{
printf {'dayfile_$tday'} ("%s,%d\n", $x,$y);
}
for ($day=1; $day<=7; $day++){
close('dayfile_$day');
}
You can't use plain strings as variables like you're trying to do.
The simplest way I can think of for this is to use a hash to map file names with filehandles.
Here's an example of how you could do that:
use strict;
use warnings;
my %files;
foreach my $i (1..7) {
my $fh;
open($fh, '>', "day_$i.log") || die "Ooups: $i: $!";
$files{$i} = $fh;
}
foreach my $i (1..7) {
my $fh = $files{$i};
print $fh "hello $i\n";
}
foreach my $i (1..7) {
my $fh = $files{$i};
close $fh;
}