in reply to use of uninitialized value in hash element problem

You are telling the program that when you split your line on '::', you will get two variables. So the program gives you two variables, even if one or both of them are empty. Then when you try to use the variable $key, it complains because $key is "uninitialized."

This is Perl being helpful since if it did not warn you, you wouldn't know that $key is not defined and that therefore the next line is not going to accomplish what you think.

Just make the next statement conditional on $key and $value having been initialized and populated. How you check that depends on whether you are concerned that either or both of the variables are defined, or are non-empty, or meet some other requirement.

UPDATE: removed a bunch of examples that could have led OP in the wrong direction, thx Alexander...

If you only want to populate your hash when both $key and $value contain text, you could do something like:

my %data_ids; my $count = 0; while ( # loop through lines from file ) { $count++; chomp $readline; my ($key , $value) = (split /::/, $readline); if (defined $key and $key ne '' and defined $value and $value ne ' +') { $data_ids{$key} = $value; } else { warn "No key-value pair from line $count: $readline\n"; } } # end loop

Also, you don't say whether you have been given this config file or whether you are building it yourself. If you have control over the file, consider using https://metacpan.org/pod/Config::Tiny which will take a config file in standard .ini format ( key=value ) and return the config to you in a hashref - with no blank lines!

Replies are listed 'Best First'.
Re^2: use of uninitialized value in hash element problem
by afoken (Chancellor) on Jun 26, 2015 at 13:06 UTC
    chomp $readline; my ($key , $value) = (split /::/, $readline); defined $key and $data_ids{$key} = $value;

    Which is the same as saying:

    chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids{$key} = $value if $key;

    Which is the same as saying:

    chomp $readline; if ( my ($key , $value) = (split /::/, $readline) ) { $data_ids{$key} = $value; }

    Sorry, but that's wrong. The three code variants have similar, but not identical effects:

    The first one stores any defined key (and value) in %data_ids, including the empty string and 0 (zero), but excluding undef. The second one stores only those keys in %data_ids which are true in boolean context. This excludes the empty string, 0, and undef. The third variant stores any key as long as split returns at least one value. This includes the empty string, 0, and undef.

    Also, the scope of $key and $value is limited to the if block in the third variant, but not in the first and second variant.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Agreed. Should not have said "the same as." Tried to oversimplify.

Re^2: use of uninitialized value in hash element problem
by mrityunjaynath (Acolyte) on Jun 26, 2015 at 12:06 UTC

    thanks lnickt....your suggestion helped and now its working fine. yes i have control over config file as i have made it and i will definetly go through the link you have givern me. thanks again