Solved

Cannot put extra delimiter after fetching null value in perl

Posted on 2012-04-09
4
219 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
problem with using the glob function 1 74
Move Function in Perl Script 2 72
.properties file to call function/method 9 61
perl getopt long help 34 71
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

856 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