uninitialized string :

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zcabeli
    New Member
    • Jan 2008
    • 51

    uninitialized string :

    Hi

    Code:
    my $x = "1-6"; 
    if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
       print "SUCCEED \n"; 
    } else { 
       print "FAILED \n"; 
    }
    i've got this simple regexp code. when i run it with use strict i get the following warning msg: "Use of uninitialized value in concatenation (.) or string .." at the line of the regexp match pattern. can anybody tell me what did i do wrong ??

    thanks,
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    You have an extra "$" in your regular expression. The $ signfies the end of line. Change it to be like this:

    Code:
    if ($x =~ /^\s*([0-9]+)\-([0-9]+)\s*$/) {
    and the error goes away.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by zcabeli
      Hi

      Code:
      my $x = "1-6"; 
      if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
         print "SUCCEED \n"; 
      } else { 
         print "FAILED \n"; 
      }
      i've got this simple regexp code. when i run it with use strict i get the following warning msg: "Use of uninitialized value in concatenation (.) or string .." at the line of the regexp match pattern. can anybody tell me what did i do wrong ??

      thanks,

      As Jeff pointed out, the extra $ in the regexp is causing the problem. The perl variable $\ is the output record seperator which has a value of undef by default. Thus you get the "use of uninitialized.. ." warning. Do this and the warning goes away:

      Code:
      use strict;
      use warnings;
      $\ = "";# <-- $\ is now initialized as an empty string
      my $x = "1-6"; 
      if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
         print "SUCCEED \n"; 
      } else { 
         print "FAILED \n"; 
      }
      but of course the regexp is then looking for s* (zero or more 's') instead of \s* (zero or more spaces) but because there are no 's' characters after the digits the regexp still matches. I suspect you just want to remove that '$' as Jeff has recommended.

      Comment

      • zcabeli
        New Member
        • Jan 2008
        • 51

        #4
        Hi,

        i accidently placed the extra $. removing it solved the problem.

        thanks,

        Comment

        Working...