Solved

Cannot put extra delimiter after fetching null value in perl

Posted on 2012-04-09
4
216 Views
Last Modified: 2012-04-09
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;
--------------------------------------------------------------------------------------
0
Comment
Question by:sunny82
  • 2
  • 2
4 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37824392
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.
0
 

Author Comment

by:sunny82
ID: 37824448
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?
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 37824463
Yes, you have to supress the warning messages by doing this:


while(my $row1=$sth1->fetchrow_arrayref())
{
no warnings;
print FILE join(";", @$row1) . "\n";
}
0
 

Author Comment

by:sunny82
ID: 37824562
Great many thanks :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now