SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pattern Matching

    Hi there I was trying to use pattern matching, the part where I need is to show just the last names that start with Ker , not sure if anyone can help me out but I'd really appreciate it if anyone could, thank you.

    I attached here below the data file.

    Code Perl:
    __DATA__
    Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
    Lesle Kerstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
    JonDeLoach:408-253-3122:123 Park St., San Jose, CA 94086:7/25/53:85100
    Ephram Hardy:293-259-5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
    Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
    William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
    Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
    James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
    Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
    Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You want the whole row when the last name starts with Ker?

    And what language are you working with?

  3. #3
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I'm trying to get just the last name printed. I'm using perl, thank you for any help.

    Quote Originally Posted by Dan Grossman View Post
    You want the whole row when the last name starts with Ker?

    And what language are you working with?

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Next time, first please post the code you have written and tried to solve your programming requirements. Here is one way to do what you are asking. Apply your own file I/O as required instead of using DATA. See any regexp tutorial for explanations of the various symbols used in the code.

    Code:
    while(<DATA>){
       if (/^\w+\s+(Ker\w*):/) {
          print "The lastnanme is $1\n";
       }
    }		
    __DATA__
    Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
    Lesle Kerstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
    JonDeLoach:408-253-3122:123 Park St., San Jose, CA 94086:7/25/53:85100
    Ephram Hardy:293-259-5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
    Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
    William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
    Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
    James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
    Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
    Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

  5. #5
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KevinR View Post
    Next time, first please post the code you have written and tried to solve your programming requirements. Here is one way to do what you are asking. Apply your own file I/O as required instead of using DATA. See any regexp tutorial for explanations of the various symbols used in the code.

    Code:
    while(<DATA>){
       if (/^\w+\s+(Ker\w*):/) {
          print "The lastnanme is $1\n";
       }
    }		
    __DATA__
    Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
    Lesle Kerstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
    JonDeLoach:408-253-3122:123 Park St., San Jose, CA 94086:7/25/53:85100
    Ephram Hardy:293-259-5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
    Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
    William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
    Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
    James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
    Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
    Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500
    ah many thanks, i was originally started with:

    Code Perl:
    while($inputline=<DATA>) {
     ($name, $phone, $address, $csz, $date, $time) = split(":", $inputline);
    }

  6. #6
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jlisec01 View Post
    ah many thanks, i was originally started with:

    Code Perl:
    while($inputline=<DATA>) {
     ($name, $phone, $address, $csz, $date, $time) = split(":", $inputline);
    }
    That can work, but all you said was you want to display the last name (that begins with Ker) from the lines of data you posted. If your goal is a bit grander than just displaying the last name you can explain what you are trying to do and maybe someone can suggest a solution better fit to your requirements.

    My code won't work for this line though:

    JonDeLoach: <- no first name?

    But maybe that was a typo on your part and is really:

    Jon DeLoach:

  7. #7
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KevinR View Post
    That can work, but all you said was you want to display the last name (that begins with Ker) from the lines of data you posted. If your goal is a bit grander than just displaying the last name you can explain what you are trying to do and maybe someone can suggest a solution better fit to your requirements.

    My code won't work for this line though:

    JonDeLoach: <- no first name?

    But maybe that was a typo on your part and is really:

    Jon DeLoach:
    oh no, that's how the book had it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •