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!
In reply to Re: use of uninitialized value in hash element problem
by 1nickt
in thread use of uninitialized value in hash element problem
by mrityunjaynath
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |