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

hii robby_dobby thanks for the suggestion. have applied those and the code is working without any warnings, but its not producing the output as desired. in the code i have opened config.txt, where you have suggested changes, in config file i have written chipid 1925 subversid 0001 and want only to read numeric value and use them but the code is picking only chipid string and using it. please tell me how should i write the code so that only numeric value is picked from config file please fine the code portion below

if(open (my $CFILE,"config.txt" ) || die "couldnt open file:", $! ) { $readline = <$CFILE>; if (defined $readline ) { @words = split(/\b[0-9][0-9][0-9][0-9]\b/,$readline); $readline = <$CFILE>; } $cChipId = $words[0]; $cSubVersId = $words[1]; print "$cChipId \n"; print "$cSubVersId \n"; }

Replies are listed 'Best First'.
Re^3: warning: use of uninitialized value
by Athanasius (Archbishop) on Jun 25, 2015 at 08:20 UTC

    Hello mrityunjaynath,

    If you say split(/X/, $string), then $string is split into chunks separated by whatever is matched by X. For example, in this one-liner:

    18:05 >perl -MData::Dump -wE "my $s = 'Hello1234world!5678How9012are34 +56you?'; my @w = split /[0-9]{4}/, $s; dd \@w;" ["Hello", "world!", "How", "are", "you?"] 18:06 >

    the string is split on 4-digit groups, and the array @w is populated with the words in $s which come between those digits.

    To capture the digit groups, you could try to split on non-digits, but it’s much better (and more straightforward) to use a regular expression with a /g modifier, like this:

    open(my $CFILE, '<', 'config.txt') or die "Couldn't open file 'config. +txt' for reading: $!"; $readline = <$CFILE>; @words = $readline =~ /([0-9]{4})/g;

    as demonstrated in this one-liner:

    18:06 >perl -MData::Dump -wE "my $s = 'Hello1234world!5678How9012are34 +56you?'; my @w = $s =~ /([0-9]{4})/g; dd \@w;" [1234, 5678, 9012, 3456] 18:18 >

    See split and perlretut.

    Hope that helps,

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

      hii athanasius i have tried something on the above code . its working but when i have printed $chipID it prints cChipId and $cSubVersId gives 1925 (in last two lines of code ) while the value i have provided through config text ChipId 1925 and SubVersId 0003. so its problemetic output. no warning this time though

      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>; chop($readline); } $cChipId = $words[0]; $cSubVersId = $words[1]; print "$cChipId \n"; print "$cSubVersId \n"; }
      thanks

        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,

Re^3: warning: use of uninitialized value
by robby_dobby (Hermit) on Jun 25, 2015 at 08:04 UTC
    Hello,

    Athanasius mentions a good point about using chomp instead of chop. I haven't completely looked at your code when I spotted those few errors.

    If others haven't got around to it before then, I'll reply back to you when I find the time.