in reply to Re^9: warning: use of uninitialized value
in thread warning: use of uninitialized value

hello robby_dobby... thanks for the help..i am at the initial level so your suggestion are helping a lot
i have tried your suggestion and changed the code like this

my $cChipId; my $cSubVersId; my %data_ids; my $readline; if(open (my $CFILE,"config.txt" ) || die "couldnt open file:", $! ) { while ($readline = <$CFILE>) { chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids($key) = $value; $readline = <$CFILE>; } foreach my $key (keys %data_ids) { $cChipid = $data_ids('chipid'); $cSubVersId = $data_ids('subversid'); } } close($CFILE);

but its showing some error like
global symbol "$data_ids" requires explicit package name at newperlfile2.pl line85. syntax error at newperlfile2.pl line 85, near "$data_ids("
....please suggest...

Replies are listed 'Best First'.
Re^11: warning: use of uninitialized value
by robby_dobby (Hermit) on Jun 26, 2015 at 09:11 UTC
    Hello,

    There are 3 problems I can spot right away:

    • You do the if(open(my $CFILE, ...) within the if block. This means that $CFILE is out of scope outside that block and you can't close it. Place the open on its own line like you did for opening dirhandles.
    • Hashes need curlies, not parens. Read my code once again: $data_ids{'chipId'}, not $data_ids('chipid'), which translates to a subroutine call.
    • Why are you doing the $readline = <$CFILE> once again? You'd be skipping between lines. Like this: read line 1, skip line2, read line 3 and so on. You're already doing it in a while loop, you can delete the second $readline = ..

    I'd reiterate once again - work through an introductory perl tutorial like perlintro. You can avoid these kind of mistakes.

      thanks robby_dobby i have applied your suggestions and its working fine by providing the required output
      but only one warning is comming out everytime
      use of uninitialized value $key in hash element at newperlfile2.pl line 85, <$CFILE> line 3.....
      the line 85 being marked with ****

      my $cChipId; my $cSubVersId; my %data_ids; my $readline; if(open (my $CFILE,"config.txt" ) || die "couldnt open file:", $! ) { while ($readline = <$CFILE>) { chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids{$key} = $value;**** } for my $key (keys %data_ids) { $cChipId = $data_ids{'chipid'}; $cSubVersId = $data_ids{'subversid'}; } }
      if result is correct why the warning keeps on comming.
      i am going through perlintro doc ..thanks

        It's possible that you have blank lines or lines with just one part of the key like: 'chipId::'. You can skip those by checking for them explicitly or something like: next if $readline =~ /^$/;.

        That said, you have ignored my first point entirely -- Do Not enclose the open in an if condition check.

        This has already gotten really long - I'm not going to continue anymore. If you still have any more problems, open a new post and others will pick up where we left off.