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

Ok, so I understand that the first line of the file “config.txt” is as follows:

chipid 1925 subversid 0003

It seems you have now chosen to split on whitespace; in which case, it’s safer to use:

@words = split /\s+/, $readline;

as this will split on one or more consecutive spaces (or tabs, etc.). This will result in the array @words containing four elements: chipid, 1925, subversid, and 0003. The following one-liner demonstrates this:

23:29 >perl -MData::Dump -wE "my $s = qq[chipid 1925 subversid 0003\n] +; my @w = split /\s+/, $s; dd \@w;" ["chipid", 1925, "subversid", "0003"] 23:30 >

So, in this case (and remembering that array subscripts begin at zero), the values you want are in $words[1] and $words[3].

BTW, why do you read the second line from $CFILE into $readline, when that second line is never used?

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^6: warning: use of uninitialized value
by u65 (Chaplain) on Jun 25, 2015 at 17:14 UTC

    I think the Cookbook recommends an even easier and more reliable method for splitting on white space:

    my @tokenarray = split ' ', $anystring;

      You don't even need to go as far as the cookbook for that, it's right there in the documentation for split:

      As another special case, split emulates the default behavior of the command line tool awk when the PATTERN is either omitted or a literal string composed of a single space character (such as ' ' or "\x20" , but not e.g. / / ). In this case, any leading whitespace in EXPR is removed before splitting occurs, and the PATTERN is instead treated as if it were /\s+/ ; in particular, this means that any contiguous whitespace (not just a single space character) is used as a separator. However, this special treatment can be avoided by specifying the pattern / / instead of the string " " , thereby allowing only a single space character to be a separator. In earlier Perls this special case was restricted to the use of a plain " " as the pattern argument to split, in Perl 5.18.0 and later this special case is triggered by any expression which evaluates as the simple string " " .

        Right you are! And that special behavior from split ' ' is why early on I made that my standard ws splitting idiom except in unusual cases.

Re^6: warning: use of uninitialized value
by mrityunjaynath (Acolyte) on Jun 26, 2015 at 04:00 UTC
    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

      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