By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

uninitialized string :

P: 51
Hi

Expand|Select|Wrap|Line Numbers
  1. my $x = "1-6"; 
  2. if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
  3.    print "SUCCEED \n"; 
  4. } else { 
  5.    print "FAILED \n"; 
  6. }
  7.  
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,
Jul 13 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
You have an extra "$" in your regular expression. The $ signfies the end of line. Change it to be like this:

Expand|Select|Wrap|Line Numbers
  1. if ($x =~ /^\s*([0-9]+)\-([0-9]+)\s*$/) {
  2.  
and the error goes away.

Regards,

Jeff
Jul 13 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Hi

Expand|Select|Wrap|Line Numbers
  1. my $x = "1-6"; 
  2. if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
  3.    print "SUCCEED \n"; 
  4. } else { 
  5.    print "FAILED \n"; 
  6. }
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. $\ = "";# <-- $\ is now initialized as an empty string
  4. my $x = "1-6"; 
  5. if ($x =~ /^\s*([0-9]+)\-([0-9]+)$\s*$/) {
  6.    print "SUCCEED \n"; 
  7. } else { 
  8.    print "FAILED \n"; 
  9. }
  10.  
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.
Jul 14 '08 #3

P: 51
Hi,

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

thanks,
Jul 14 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.