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

Hi monks, I am back with another homework assignment - BUT I have done it all but somehow cant figure out why an array is not processing correctly - I need to get user input as comma delimited text or digits (i.e. dogs, cats, 4, 8) and then iterate the array and divide the digts if any by 2 and then print the whole thing out...I have gone over this a ton of times and can't figure out why it wont divide the digits (if any) by 2...here is the piece of code I wrote
print "Enter the Comma Separated Text...:"; $str = <STDIN>; chomp($str); #Remove the quotes $str=~s/"//g; # @arr now contains the individual values @arr=split(",",$str); for($i=0;$i<=$#arr;$i++){ # if the array contains only digits if($arr[$i]=~/^d+$/){ $arr[$i]=$arr[$i]/2; } } print "Values in the Array are as Follows\n"; #print that Array foreach $val(@arr){ print "$val\n"; }
the other portion of my problem is using using a regular expression on a user-input phone number - when I ask to print out the variable all I get is "+" instead of the whole phone number
#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;
now I know you frown on homework help but if you could just tease me with a hint or two in the right direction...am I totally off track on these or am I in the neighborhood...? once again thanks for the wisdom monks sierra

Replies are listed 'Best First'.
Re: wisdom needed for string matching and array processing
by friedo (Prior) on Nov 06, 2005 at 23:40 UTC
    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; } }
      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
        --
Re: wisdom needed for string matching and array processing
by Fletch (Bishop) on Nov 06, 2005 at 23:41 UTC

    Given the example text you did it appears that you've got "dogs, cats, 4, 8". When you split on "," you'll have spaces at the beginning of your items, hence your anchored regex will fail (e.g. " 4" will not match /^\d+$/). You either need to allow optional spaces there or add them to your split delimiter so they're removed when you split. See perldoc perlre in either case.

      thank you! I added
      $str=~s/\s+//g;
      and I removed any whitespace... I know perl is soooo easy for you monks but I struggle...it doesn't help that the course is online and I never really get instruction...but I am making slow progress! thanks again! sierra