Link to home
Start Free TrialLog in
Avatar of sunny82
sunny82

asked on

Cannot put extra delimiter after fetching null value in perl

Hi,

I have a problem. In this code below, I am getting a “semi-colon” delimited csv file from a table. But if last column has null value, it is not putting that extra semi-colon at the end.

So we are getting results as (D is null here in database)

A;B;C;D
1;2;3;

instead of 1;2;3;;

Where am I going wrong??
----------------------------------------------------------------------------------------------------
my $sth1= $dbh->prepare($query) or die "Unable to prepare the statement";

$sth1->execute();

my $text_file = "$ENV{'DATA'}" .  $run_date . "_" . "XYZ.dat";

open(FILE, "> $text_file") or die "Unable to open the file";

my $header=$sth1->{NAME};

print FILE join(";", @$header) . "\n";

while(my $row1=$sth1->fetchrow_arrayref())

{
 
print FILE join(";", @$row1) . "\n";


}

close(FILE);

$sth1->finish;
--------------------------------------------------------------------------------------
Avatar of farzanj
farzanj
Flag of Canada image

Nothing wrong.  This is the way it is supposed to behave.  Look carefully, even in the header, there are only 3 semi colons and nothing after the last column.  So it is consistent and correct.
Avatar of sunny82
sunny82

ASKER

Ok thank you. I am also getting a warning ----

Use of uninitialized value $row1 in join or string at XYZ.pl line 70.
(print FILE join line)

Is this also normal? What can I do to prevent this?
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunny82

ASKER

Great many thanks :)