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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.