sierrastar has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, Can anyone tweek me in the right direction as to why my match variables do not seem to be storing a value? When the variable "$phone" prints out it only prints "+"... thanks so much monks - sierra
while($phone =~ /D/ || $len !=10){ print "Please enter your 10 digit phone number:"; $phone = <STDIN>; chomp($phone); #Find out the length of the string $len=($phone=~tr/[0-9]/[0-9]/); } #stor in $1 $2 $3 and format for international phone format $phone=~/ddd)(ddd)(dddd)/; $formattedphone="+"."1"." ".$1." ".$2." ".$3;

Replies are listed 'Best First'.
Re: regular expressions and match variables
by sauoq (Abbot) on Nov 07, 2005 at 01:42 UTC

    I believe someone told you in your previous thread on this that you need to use "\d" and "\D" in your expressions, not just "d" or "D" (which match literal characters.) As such, your expression isn't matching anything and, consequently, isn't catching anything in $1, $2, and $3. So, when you print $formattedphone all it contains is the literal "+" you have assigned to it.

    -sauoq
    "My two cents aren't worth a dime.";
    
      HI, I changed the whole thing to this
      $newphone = "+"."1"." ".substr($phone,0,3)." ".substr($phone,3,3)." ". +substr($phone,6,4);
      and just got rid of the match variables....and it worked - thanks so much!