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

hi Athanasius,

actually the config file read like this ChipId 1925 SubVersid 0001 thats the reason i have read second line... what i want is to take only numeric value but its not happening the code being the same that i have posted you

my @words; my $readline; my $cChipId; my $cSubVersId; if(open (my $CFILE,"config.txt" ) || die "couldnt open file:", $! ) { $readline = <$CFILE>; chop($readline); if (defined $readline ) { @words = split(/::/,$readline); $readline = <$CFILE>; } $cChipId = $words[0]; $cSubVersId = $words[1]; print "$cChipId \n"; print "$cSubVersId \n"; }
when i am reading $words[0] and $words1 its giving chipid and 1925...ok .....but when i am reading $words1 and $ words3 it takes nothing for $words3 and shows warning line uninitialized value for $cSubVersId....please help

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

    Athanasius gave you the right advice about picking the correct values off split. What you need to realize is, arrays are indexed from zero: if you have an array @myarray with 3 elements, it will have elements at: $myarray[0], $myarray[1] and $myarray[2]. Also, why are you splitting on :: when you have specified the config file to be delimited by spaces?

    $ cat pm_1132055.pl #!perl while(my $line = <DATA>) { chomp $line; my @words = split /\s+/, $line; print join(", ", @words), "\n"; print qq{\$words[1] --> $words[1] and \$words[3] --> $words[3]}, " +\n"; } __DATA__ ChipId 1925 SubVersid 0001 $ perl pm_1132055.pl ChipId, 1925, SubVersid, 0001 $words[1] --> 1925 and $words[3] --> 0001
    Now, consider what you have done:
    $ cat pm_1132055.pl #!perl while(my $line = <DATA>) { chomp $line; my @words = split /::/, $line; print join(", ", @words), "\n"; print qq{\$words[1] --> $words[1] and \$words[3] --> $words[3]}, " +\n"; } __DATA__ ChipId 1925 SubVersid 0001 $ perl pm_1132055.pl ChipId 1925 SubVersid 0001 $words[1] --> and $words[3] -->

      my fault...i have less knowledge of html so not able to write properly..what i want to say is my config file reads like this,
      chipId::1925
      subversid::0001
      now this is creating the above stated problem

        Well, in that case - you can do something like this:
        #!perl my %data_ids; while(my $line = <DATA>) { chomp $line; my ($key, $value) = split /::/, $line; $data_ids{$key} = $value; print $key, " => ", $value, "\n"; } for my $key (keys %data_ids) { print $key, " ==> ", $data_ids{$key}, "\n"; } # or just print $data_ids{'chipId'}, "\n"; print $data_ids{'subversid'}, "\n"; __DATA__ chipId::1925 subversid::0001

        From your original code sample, I surmise that you need these IDs for generating an m4 macro. Using a hash might be useful for you since you can just look that up when generating the m4 file later on. Bear in mind all points raise in this thread, use close, closedir properly.

        This got longer than expected. Next time around, you could save us all a lot of back and forth by including a small data sample as I showed above with __DATA__. Also, work through perlintro or an introductory perl book(learn.perl.org lists quite a few of them and some freely available online) and ask more questions here when you get stuck.

        Good luck!