"defined $r_libs->{$name} " triggers warning "Use of uninitialized value"

Discussion in 'Perl' started by Liang, Aug 9, 2004.

  1. Liang

    Liang Guest

    Hi,

    I use "defined $r_libs->{$name}" to check first if a key exists in a hash
    table. But Perl gives a warning WHENEVER the key exists: "Use of
    uninitialized value".

    Would u please help to check the script, and let me know the reason? Thanks
    in advance.

    Liang

    ----------------------------------
    #!/usr/local/bin/perl -w

    my %libraries;

    #$obj_name='obj1';
    #$sect_name='sect1';
    #$len='001a2';

    print "KKK\n";


    add_one_libobj(\%libraries, 'obj1', 'sect1', '001a2');
    add_one_libobj(\%libraries, 'obj2', 'sect2', '002a2');

    add_one_libobj(\%libraries, 'obj1', 'sect2', '002a2');

    add_one_libobj(\%libraries, 'obj3', 'sect3', '003a2');

    add_one_libobj(\%libraries, 'obj1', 'sect3', '003a2');

    print "@@@\n";

    print_lib_res (\%libraries);

    print "HHH\n";


    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # put the result into hash table
    #----------------------------------------
    sub add_one_libobj
    {
    my ($r_libs, $name, $sect, $len) = @_;

    print "add_one_libobj:: %$r_libs, $name\n";

    #
    # the following line will trigger an Perl warning:
    # Use of uninitialized value at map_reader.pl
    # do u know what's wrong? thanks. Liang
    #
    if ( defined $r_libs->{$name} ){
    #print " the library exists:: $r_libs->{$name}\n";
    $r_libs->{$name}->{$sect} += hex "0x$len";
    }else{
    my %rec;
    $rec{$sect}= hex "0x$len";
    $r_libs->{$name}=\%rec;

    # print " the library is new:: %$r_libs->{$name}\n";
    }
    }

    sub print_lib_res
    {
    my ($r_libs) = @_;
    my ($name, $sect, $len) ;

    #print"print_lib_res:: $r_libs\n";

    foreach $name (sort keys %$r_libs){
    my $r_sect = %$r_libs->{$name};

    #print "name=$name, sect = $r_sect\n";
    foreach $sect ( sort keys %$r_sect ){

    print <<MSG_1
    lib/obj=$name, sect=$sect, len=$r_sect->{$sect};
    MSG_1
    ;

    }
    }

    }
    -----------------------------------
    output:
    -----------------------------------
    KKK
    add_one_libobj:: %HASH(0x825c68), obj1
    add_one_libobj:: %HASH(0x825c68), obj2
    add_one_libobj:: %HASH(0x825c68), obj1
    Use of uninitialized value at tt.pl line 46.
    add_one_libobj:: %HASH(0x825c68), obj3
    add_one_libobj:: %HASH(0x825c68), obj1
    Use of uninitialized value at tt.pl line 46.
    @@@
    lib/obj=obj1, sect=sect1, len=418;
    lib/obj=obj1, sect=sect2, len=674;
    lib/obj=obj1, sect=sect3, len=930;
    lib/obj=obj2, sect=sect2, len=674;
    lib/obj=obj3, sect=sect3, len=930;
    HHH
    Liang, Aug 9, 2004
    #1
    1. Advertising

  2. Liang

    Joe Smith Guest

    Re: "defined $r_libs->{$name} " triggers warning "Use of uninitializedvalue"

    Liang wrote:

    > Would u please help to check the script, and let me know the reason? Thanks
    > in advance.


    Your script works with perl-5.8.3 after changing one line to
    my $r_sect = $r_libs->{$name};
    by removing the extraneous % sign.

    Try with other versions of perl, and post your problem to
    comp.lang.perl.misc not comp.lang.perl .
    -Joe
    Joe Smith, Aug 9, 2004
    #2
    1. Advertising

  3. Liang

    Liang Guest

    "Joe Smith" <> wrote in message
    news:86GRc.231703$IQ4.70113@attbi_s02...
    > Liang wrote:
    >
    > > Would u please help to check the script, and let me know the reason?

    Thanks
    > > in advance.

    >
    > Your script works with perl-5.8.3 after changing one line to


    Thanks Joe. You're right. It works on 5.6.0 as well. The problem occuses on
    5.001.

    > my $r_sect = $r_libs->{$name};
    > by removing the extraneous % sign.
    >
    > Try with other versions of perl, and post your problem to
    > comp.lang.perl.misc not comp.lang.perl .
    > -Joe
    Liang, Aug 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,222
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,232
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    5,984
    Joe Smith
    Jun 9, 2004
  4. Sunil
    Replies:
    3
    Views:
    98
    Sunil
    Oct 6, 2003
  5. S
    Replies:
    3
    Views:
    320
    Gunnar Hjalmarsson
    Feb 3, 2004
Loading...

Share This Page