Why does script writes output to screen and not to the out put file

A question to you - wilcoxon,

This question is in reference to (https://www.experts-exchange.com/questions/29079671/How-to-generate-multiple-files-from-a-perl-script.html)

This question is related to related to a question you helped resolve lately.  So Am addressing this to you as you may be able to easily identify the issue.

The script below writes contents to the cmd screen when run.  For some reason, the data in not getting written to a out file. I can see the data dump  from the database table on the cmd screen but the outfile generated is only listing out the column headers, but no data is generated to the file?


use strict;
use warnings;
use DBI;
use Date::Manip;

Date_Init('TZ=US/Central','Internal=1');

$| = 1;

my $nodata = 0;
my ($db, $user, $pw, $wkdir, $firstday, $lastday)  = @ARGV;

# Display parameter values passed by the appworx

print("\n");
print("Database Instance  : $db \n");
print("Connected as User  : $user \n");
print("Output Directory is : $wkdir \n");
print("First Day : $firstday \n");
print("Last Day : $lastday \n\n");

#Check to see if required parameters are received by appworx
die("\n\n  - ERROR:  The Required number of arguments were not received, PROCESS ABORTING!!!\n\n") unless $lastday;

#The condition below checks to see if the month for first/last are the same and if not the same then checks to see if the years are the same.

my ($fyr, $fmon, $fday, $lyr, $lmon, $lday);
if ($firstday =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
    ($fyr, $fmon, $fday) = ($1, $2, $3);
} else {
    die " - ERROR: firstday ($firstday) is not a date\n";
}
if ($lastday =~ /^(\d\d\d\d)(\d\d)(\d\d)$/) {
    ($lyr, $lmon, $lday) = ($1, $2, $3);
} else {
    die " - ERROR: lastday ($lastday) is not a date\n";
}

# Connectivity check to the database else display failed connectivity
my $dbh = DBI->connect("dbi:Oracle:$db", $user, $pw, { AutoCommit => 0, RaiseError => 1 }) or die("Cannot connect to Database '$db' " . DBI->errstr());


# Capture counts for distinct partitions for given firstday and lastday dates
my $partition_key = "${firstday}_$lastday";
print("Partition Key : $partition_key \n\n");

# Get value of rowcount returned from Count of partition keys from REGION table
my ($rowcount) = $dbh->selectrow_array("select count(distinct r.execution_id) from REGION r where r.partition_key = '$partition_key' and r.status = 'N'");

# Check if single partition exists for a given firstday/lastday parameters, ROW COUNT RETURNED will be 1 and then proceed with extract out columns from the table to a csv file
# IF multiple partitions exist OR no data exists for a given firstday/last day set - the PROCESS WILL ABORT

die "ERROR: NO DATA EXISTS FOR GIVEN FIRSTDAY LASTDAY SET OF DATES - ABORTING!!!\n\n"
    unless $rowcount;

die "ERROR: UNREPORTED DATA EXISTS FOR THE GIVEN FIRSTDAY LASTDAY SET OF DATES - ABORTING!!!\n\n"
    if ($rowcount > 1);

# Datasets where each data set is the file to be processed.  The script will output a total of 5 files.

do_dataset('Detail_ESIID', [qw(ESIID REPCODE REPNAME TO_CHAR(STARTTIME,'MM/DD/YYYY') ORIGIN INTV1 INTV2 INTV3 INTV4 INTV5 INTV6 INTV7 INTV8 INTV9 INTV10 INTV11 INTV12 INTV13 INTV14 INTV15 INTV16 INTV17 INTV18 INTV19 INTV20 INTV21 INTV22 INTV23 INTV24 INTV25 INTV26 INTV27 INTV28 INTV29 INTV30 INTV31 INTV32 INTV33 INTV34 INTV35 INTV36 INTV37 INTV38 INTV39 INTV40 INTV41 INTV42 INTV43 INTV44 INTV45 INTV46 INTV47 INTV48 INTV49 INTV50 INTV51 INTV52 INTV53 INTV54 INTV55 INTV56 INTV57 INTV58 INTV59 INTV60 INTV61 INTV62 INTV63 INTV64 INTV65 INTV66 INTV67 INTV68 INTV69 INTV70 INTV71 INTV72 INTV73 INTV74 INTV75 																INTV76 INTV77 INTV78 INTV79 INTV80 INTV81 INTV82 INTV83 INTV84 INTV85 INTV86 INTV87 INTV88 INTV89 INTV90 INTV91 INTV92 INTV93 INTV94 INTV95 INTV96 INTV97 INTV98 INTV99 INTV100 TOTALS)], status => "'N'");
do_dataset('Monthly_ESIID', [qw(ESIID REPCODE REPNAME SUM(TOTALS))] , group_by => [qw(GROUP BY ESIID, REPCODE, REPNAME)]);
do_dataset('Daily_REP', [qw(REPNAME  REPCODE TO_CHAR(STARTTIME,'MM/DD/YYYY') SUM(TOTALS) SUM(SUBTRACT_TOTAL) SUM(ADJ_TOTAL))], group_by => [qw(GROUP BY REPNAME, REPCODE, TO_CHAR(STARTTIME,'MM/DD/YYYY'))]);
do_dataset('Monthly_REP', [qw(REPNAME REPCODE TO_CHAR(STARTTIME,'MM/DD/YYYY') SUM(SUBTRACT_TOTAL) SUM(ADJ_TOTAL))], group_by => [qw(GROUP BY REPNAME, REPCODE, TO_CHAR(STARTTIME,'MM/DD/YYYY'))]);
do_dataset('RecLoad', [qw(REPNAME DUNSNUMBER TO_CHAR(STARTTIME,'MM/DD/YYYY') SUM(ADJ_TOTAL))], group_by => [qw(GROUP BY REPNAME, DUNSNUMBER, TO_CHAR(STARTTIME,'MM/DD/YYYY'))]);

# Disconnect from the database
$dbh->disconnect();

# Subroutine does processing of the 5 datasets
sub do_dataset 
 {
    my ($file, $cols, %opt) = @_;
    
    my $col = join ',', @$cols;
    my $group = ' ';
    
    # process the below if dataset has a group by    
    if (exists $opt{group_by}) 
    {
        $group = join ' ', @{$opt{group_by}};
        delete $opt{group_by};
    }
    
    # The below statement is placeholder to hold extra clauses or conditions, for instance dataset1 would have print a value of STATUS = 'N' 
     
    my $extra = %opt ? ('and ' . join(' and ', map { "$_ = $opt{$_}" } keys %opt)) : '';
     																

    # Fetch data from the table
    my $allrows = $dbh->selectall_arrayref("SELECT $col FROM REGION WHERE PARTITION_KEY = '$partition_key' $extra $group")
        or die("Could not get data from table REGION ".DBI->errstr);

    # dump the data out to a file
    my $dumpdir = "$wkdir/$file.csv";
    open OUT, '>', $dumpdir or die " - Could not open $dumpdir for export. $!";
    print OUT $col, "\n"; # manually list column names from input
    print OUT map { print join(',', map { defined($_) ? $_ : '' } @$_), "\n" } @$allrows;
    close OUT;

    # Updates the STATUS column to R(Reported) for the partition key after REC LOAD csv outfile is generated
    print "Updating data set status to R (reported) for partition key $partition_key. \n\n";
    $dbh->do("UPDATE REGION SET STATUS = 'R', LSTIME = SYSDATE WHERE PARTITION_KEY = '$partition_key' $extra")
          or die "Could not get data from table REGION ".DBI->errstr;

# File output process completion 
    print "The $dumpdir file is generated to output directory.\n\n";
}

Open in new window

steve2312Asked:
Who is Participating?
 
wilcoxonCommented:
Remove the print inside the braces on line 98 and it should work.  If it doesn't, let me know.
print OUT map { join(',', map { defined($_) ? $_ : '' } @$_), "\n" } @$allrows;

Open in new window

0
 
steve2312Author Commented:
Thanks it worked. Appreciate  your quick help on this wilcoxon
0
 
steve2312Author Commented:
Thanks that worked. Appreciate  your quick help on this wilcoxon!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.