#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Smile Determining if a line contains all letters


    Hi,

    I'm trying to write a program that asks the user to input the direct path to a file and read through each line. All those parts are fine, but I also want to count the letters ONLY in lines that contain all letters. For example, I want to count them in this line:

    ABCDEFG

    but not this line:

    A@B#C$D

    so far, I have tried a bunch of things, mostly along the lines of:

    if ( $line =~ /[a-zA-Z]+$/ ) {
    $count += length($line)
    }

    Any advice on how I should go about or modify this to work would be greatly appreciated!

    Jess
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Your regex only checks that there is at least one letter in the line. You want to check that everything from the beginning to the end is a letter.

    Try ^ and $ anchors. You may also need to take into account line endings.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Your question is not consistent with your example.

    Do you want lines that contain only letters? If yes, try this:

    Perl Code:
    if ( $line =~ /^[a-zA-Z]+$/ ) {
         $count += length($line)
    }

IMN logo majestic logo threadwatch logo seochat tools logo