FAQ
Sorry, this isn't really a perl-dbi issue per-se. but I don't know where
else to post this issue. I can't find a "Perl" mail-list. There's loads
of list, but no perl-general.

Anyway.. pulling data from SQL-server using perl-dbi to be formatted as
a CSV file.

I had to do some tweaking to the output to maintain the format which
postgressql likes.

eg:
"A","B","C",,"D","E"

(note the ,, which means a NULL value)

My current code is below however, I enabled warnings and strict and get
this.

Use of uninitialized value in length at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 195 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a
mistake. To suppress this warning assign a defined value to your
variables.
To help you figure out what was undefined, perl tells you what
operation you used the undefined value in. Note, however, that perl
optimizes your program and the operation displayed in the warning may
not necessarily appear literally in your program. For example, "that
$foo" is usually optimized into "that " . $foo, and the warning will
refer to the concatenation (.) operator, even though there is no . in
your program.

Use of uninitialized value in concatenation (.) or string at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 197 (#1)
"A","B",,"C"
Use of uninitialized value in length at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 188 (#1)
Use of uninitialized value in print at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 190 (#1)


Code Block:
==============

##
## We do the fetching of the returned values and then we perform some
formatting prior
## to output. We need to make sure that if we are at the last value, we
must omit the
## trailing comma (,) to make it of form "value1","value2",,"value4"
## We also need to check if the current value is a NULL (0 length), if
so, we just
## do not put a quote mark around it.
##
while ( @first = $sth->fetchrow_array )
{
# my $first = 0;
# my $counter = 0;
my $count = @first;
my $first = @first;

for ($counter = 0; $counter < $count; $counter = $counter + 1)
{
if ($counter == $count-1)
{
if (length($first[$counter]) == 0) <==line 188
{
print $first[$counter]; <== Line 190
} else {
print "\"$first[$counter]\"";
}
} else {
if (length($first[$counter]) == 0)
{
print $first[$counter].","; <==LIne 197
} else {
print "\"$first[$counter]\",";
}
}
}
print ("\n");
}
}


Thanks in advance..

Search Discussions

  • Gary Stainburn at Aug 16, 2007 at 9:43 am
    Hi

    Firstly, for an excelent general perl list, try beginners@perl.org. It's not
    just for beginners.

    Secondly, it looks like you're trying to access a field that isn't defined,
    possibly by running off the end of the row.

    I've re-written (but not tested) the routine slightly more perlified.

    See what you think.

    while (my @row = $sth->fetchrow_array )
    {
    foreach my $field (shift @row) {
    if (! defined ($field) || (length($field) == 0))
    {
    # don't know why you do this cos
    # you're printing nothing here
    print $field;
    } else {
    print "\"$field\"";
    }
    if (@row) # fields left, stick in a comma
    {
    print ",";
    }
    }
    print ("\n");
    }
    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
  • Ow Mun Heng at Aug 16, 2007 at 10:41 am

    On Thu, 2007-08-16 at 10:38 +0100, Gary Stainburn wrote:
    Hi

    Firstly, for an excelent general perl list, try beginners@perl.org. It's not
    just for beginners.
    Ah.. Cool.. I'll subscribe there.
    Secondly, it looks like you're trying to access a field that isn't defined,
    possibly by running off the end of the row.

    I've re-written (but not tested) the routine slightly more perlified.

    See what you think.

    while (my @row = $sth->fetchrow_array )
    {
    foreach my $field (shift @row) {
    if (! defined ($field) || (length($field) == 0))
    {
    # don't know why you do this cos
    # you're printing nothing here
    print $field;
    } else {
    print "\"$field\"";
    }
    if (@row) # fields left, stick in a comma
    {
    print ",";
    }
    }
    print ("\n");
    }
    Thanks for the solution, however it doesn't really run. Not sure why.
    There's a supposed missing brace/curly brace somewhere but I can't find
    it yet. Your solution is more elegent. I will definitely try to figure
    out where the error is..
  • Peter J. Holzer at Aug 16, 2007 at 9:51 am

    On 2007-08-16 16:38:02 +0800, Ow Mun Heng wrote:
    Sorry, this isn't really a perl-dbi issue per-se. but I don't know where
    else to post this issue. I can't find a "Perl" mail-list. There's loads
    of list, but no perl-general.
    The newsgroup comp.lang.perl.misc is probably the best place to discuss
    general perl questions.

    Anyway.. pulling data from SQL-server using perl-dbi to be formatted as
    a CSV file.

    I had to do some tweaking to the output to maintain the format which
    postgressql likes.

    eg:
    "A","B","C",,"D","E"

    (note the ,, which means a NULL value)

    My current code is below however, I enabled warnings and strict and get
    this.

    Use of uninitialized value in length at
    /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
    line 195 (#1) [...]
    Use of uninitialized value in concatenation (.) or string at
    /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
    line 197 (#1)
    "A","B",,"C"
    Use of uninitialized value in length at
    /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
    line 188 (#1)
    Use of uninitialized value in print at
    /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
    line 190 (#1) [...]
    ##
    while ( @first = $sth->fetchrow_array )
    {
    # my $first = 0;
    # my $counter = 0;
    my $count = @first;
    my $first = @first;

    for ($counter = 0; $counter < $count; $counter = $counter + 1)
    {
    if ($counter == $count-1)
    {
    if (length($first[$counter]) == 0) <==line 188
    You really want to know whether $first[$counter] is undef, not whether
    it has a length == 0 here, so you should test for that:

    if (!defined($first[$counter]))
    {
    print $first[$counter]; <== Line 190
    Also you shouldn't try to print undefined values. Just omit the print
    here ...
    } else {
    print "\"$first[$counter]\"";
    }
    } else {
    if (length($first[$counter]) == 0)
    {
    print $first[$counter].","; <==LIne 197
    and only the comma here.
    } else {
    print "\"$first[$counter]\",";
    }
    }
    }
    print ("\n");
    }
    }
    an alternative is to use

    no warnings 'undefined';

    in the smallest enclosing block. But that is only useful if you want to
    treat undefined values exactly like empty strings, which isn't the case
    here.

    hp

    --
    _ | Peter J. Holzer | If I wanted to be "academically correct",
    _|_) | Sysadmin WSR | I'd be programming in Java.
    hjp@wsr.ac.at | I don't, and I'm not.
    __/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users
  • Ow Mun Heng at Aug 16, 2007 at 10:31 am

    On Thu, 2007-08-16 at 11:50 +0200, Peter J. Holzer wrote:
    On 2007-08-16 16:38:02 +0800, Ow Mun Heng wrote:
    Sorry, this isn't really a perl-dbi issue per-se. but I don't know where
    else to post this issue. I can't find a "Perl" mail-list. There's loads
    of list, but no perl-general.
    The newsgroup comp.lang.perl.misc is probably the best place to discuss
    general perl questions.
    No nntp for me to use. best would be mail list
    You really want to know whether $first[$counter] is undef, not whether
    it has a length == 0 here, so you should test for that:

    if (!defined($first[$counter]))
    That worked.. I was thinking of checking the length because if there the
    data is NULL, then length should be 0 and not undef.

    an alternative is to use

    no warnings 'undefined';

    in the smallest enclosing block. But that is only useful if you want to
    treat undefined values exactly like empty strings, which isn't the case
    here.
    NULL != Empty string.. Right.

    Here's the altered one, for brewity..

    my @first;
    while ( @first = $sth->fetchrow_array )
    {
    my $count = @first;
    my $first = @first;

    for ($counter = 0; $counter < $count; $counter = $counter + 1)
    {
    if ($counter == $count-1)
    {
    if (!defined($first[$counter]))
    {
    # WHAT DO I PUT HERE?
    } else {
    print "\"$first[$counter]\"";
    }
    } else {
    if (!defined($first[$counter]))
    {
    print ",";
    } else {
    print "\"$first[$counter]\",";
    }
    }
    }
    print ("\n");
    }
    }
  • Peter J. Holzer at Aug 16, 2007 at 11:36 am

    On 2007-08-16 18:30:52 +0800, Ow Mun Heng wrote:
    On Thu, 2007-08-16 at 11:50 +0200, Peter J. Holzer wrote:
    You really want to know whether $first[$counter] is undef, not whether
    it has a length == 0 here, so you should test for that:

    if (!defined($first[$counter]))
    That worked.. I was thinking of checking the length because if there the
    data is NULL, then length should be 0 and not undef.
    In SQL, length(NULL) is NULL, not 0. In Perl length(undef) is 0, but you
    get a warning.

    Here's the altered one, for brewity..

    my @first;
    while ( @first = $sth->fetchrow_array )
    {
    my $count = @first;
    my $first = @first;

    for ($counter = 0; $counter < $count; $counter = $counter + 1)
    {
    if ($counter == $count-1)
    {
    if (!defined($first[$counter]))
    {
    # WHAT DO I PUT HERE?
    Nothing. There is nothing to do in this case.
    } else {
    print "\"$first[$counter]\"";
    }
    You could invert the test to avoid the empty if:

    if (defined($first[$counter]))
    {
    print "\"$first[$counter]\"";
    }

    [...]

    Of course the whole loop can be written quite a bit shorter (and more
    "perlish"):

    print join(q{,}, map { defined $_ ? qq{"$_"} : q{} } @first), "\n";

    hp

    --
    _ | Peter J. Holzer | If I wanted to be "academically correct",
    _|_) | Sysadmin WSR | I'd be programming in Java.
    hjp@wsr.ac.at | I don't, and I'm not.
    __/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbi-users @
categoriesperl
postedAug 16, '07 at 8:51a
activeAug 16, '07 at 11:36a
posts6
users3
websitedbi.perl.org

People

Translate

site design / logo © 2022 Grokbase