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

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,

Replies are listed 'Best First'.
Re^4: warning: use of uninitialized value
by mrityunjaynath (Acolyte) on Jun 25, 2015 at 09:47 UTC

    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,

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

        my @tokenarray = split ' ', $anystring;
        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