[Chicago-talk] undef values in arrays.

Steven Lembark lembark at wrkhors.com
Mon Sep 15 15:38:18 PDT 2008

> This statement:
>  print $Count . " " . join(", ", @a) . "\n";
> Produces this errors message:
> Use of uninitialized value in join or string at ./TransferTables.pl line 4154.

Error or warning?
Using an undef shouldn't be fatal unless
someone makes it explicit (as in defined or die...
or an __WARN__ handler that dies).

> The array is a result of this statement:
> my @a = $sth_s->fetchrow_array

> I presume I have a NULL value in my database and it come back as a
> undef in the array.


> I rarely allow NULL values in a database so I don't know 
> that from experience.

Yup. If you use Oracle there ain't no other way
since they don't provide the only reasonable
default for strings: '' is converted to a NULL.

If you use Postgres, MySQL, Sybase or something
else sane you can avoid this by using the empty
string as a default.

One way out is modify the query to include
"foo is not null" or

     @rowz   = grep { $_->[ blah ] } @rowz;

> Anyway, Is there a graceful way to replace the udef values in @a with
> the string "<NULL>"?

     my $null    = '<NULL>';

     my $rowz    = $sth->fetchrow_array( ... );

     for( @$rowz )
         for( @$_ )
             defined or next;

             $_ = $null;

> That way I will see them and I will not get the warning 
> message.

Proably better off hacking the SQL to just
remove records with NULL values from being
processed and leave the warning (or more
detailed one with a key) for the records
missing values.

More information about the Chicago-talk mailing list