in reply to wisdom needed for string matching and array processing

The main problem I see is that you are using the literal character 'd' in your regexes, where you want \d. "d" matches only the character "d", whereas "\d" matches the digits 0-9. So, for example, this:

if($arr[$i]=~/^d+$/){

Should be

if($arr[$i]=~/^\d+$/){

(Although as a matter of style, I would probably rewrite the whole loop as follows:)

for( @arr ) { # if the element contains only digits if( /^\d+$/ ) { $_ /= 2; } }

Replies are listed 'Best First'.
Re^2: wisdom needed for string matching and array processing
by sierrastar (Sexton) on Nov 07, 2005 at 00:24 UTC
    thank you your code is much more eloquent than mine...but I think I am learning - do you have any suggestions as to why the phone number is only printing out a "+" here is that code again:
    #Get the phone number and check to make sure that it contains only dig +its and the length is 10 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]/); } #Using regular expressions, break down the phone number into first num +ber the #next three digits and then the next three digits and so on. Each digi +ts will be #stored in $1 $2 $3 and $4. $phone=~/(d)(ddd)(ddd)(ddd)/; $formattedphone="+".$1." ".$2." ".$3." ".$4;
    thanks

      Read the reply more carefully - the answer was given. The mistake you made in the same sample is the same as the mistake you made in the second.

      (I'm not trying to tease and withold the answer - but if it is homework then thinking about the problem and reading the reply you've recieved carefully should be a good thing for you!)

      Steve
      --