Re: Unitialized value in hash element problem...



Tony Frasketi wrote:
> Hello Listers

Hello,

> I'm getting the following error when executing the function listed below...

It is a warning not an error.


> cimagedisp_new.cgi: Use of uninitialized value in hash
> element at /.../cimagedisp_new.cgi line 238, <FILE> line 69.

perldoc perldiag
[snip]

Use of uninitialized value%s
(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.


> 223 #-------------------------------------------------------------
> 224 # Build a hash of pricelist names/ids from an inc file
> 225 # my(%plhash) = &build_pricelist_hash($plnameid_file);
> 226 #-------------------------------------------------------------
> 227 sub build_pricelist_hash {
> 228 my($file) = @_;
> 229
> 230 my(%plhash) = ();
> 231 if(!open(FILE,$file)) {
> 232 &abort_program("<b>FILE ERROR:</b><br>Can't open file
> $file<br>\n[$!]");
> 233 }
> 234 my($i) = 0;
> 235 while (<FILE>) {
> 236 chop $_;
> 237 my($name,$id) = split(/\|/,$_);
> 238 $plhash{$id} = $_;

The variable $id has the value 'undef' and is therefore producing this
warning. From the warning message it looks like line 69 of $file does
not contain the '|' character.

$ perl -le'
for ( "one|two", "one two", "one|two|three" ) {
my ( $one, $two ) = split /\|/;
print defined $one ? ">$one< " : "one=undef ",
defined $two ? ">$two< " : "two=undef ";
}
'
>one< >two<
>one two< two=undef
>one< >two<


Also, you should use chomp() instead of chop().




John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: ANNOUNCE: DBD:Oracle 1.18
    ... Well a warning in one compiler is better than and error in an other so I ... This might be a problem more with the older oracle OCI in your client. ... perl -Mblib t/26exe_array.t ...
    (perl.dbi.users)
  • AW: Problems building DBD-Oracle-1.16 on HP
    ... Looks as if gcc gets some parameters it does not understand. ... I have tried all the various incantations listed in the READMEs (perl ... Oracle version 10.2.0.2 ... Oracle.c:24: warning: 'RETVAL' might be used uninitialized in this ...
    (perl.dbi.users)
  • Help: Install DBI-Oracle on cygwin with Perl 5.8.0
    ... I am new in the Perl world. ... Xlib.xs:383: warning: cast to pointer from integer of different size ...
    (comp.lang.perl)
  • Help: Install DBI-Oracle on cygwin with Perl 5.8.0
    ... I am new in the Perl world. ... Xlib.xs:383: warning: cast to pointer from integer of different size ...
    (comp.lang.perl.modules)
  • ANNOUNCE: Win32::GUI V1.06
    ... Win32::GUI is a Perl extension allowing creation of native Win32 GUI ... controls of an MDI child window. ... The info and warning icon for balloon tooltips were the wrong ... (Tracker: 1709017) ...
    (comp.lang.perl.modules)