Solved

Cannot put extra delimiter after fetching null value in perl

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

756 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