Solved

Cannot put extra delimiter after fetching null value in perl

Posted on 2012-04-09
4
215 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

13 Experts available now in Live!

Get 1:1 Help Now