Re: String parsing (2 questions)

Discussion in 'Perl Misc' started by Jürgen Exner, Aug 29, 2012.

  1. Eli the Bearded <*@eli.users.panix.com> wrote:
    >In comp.lang.perl.misc, Robert Crandal <> wrote:
    >> My text file contains a list of strings that look similar to this:
    >>
    >> ID1 ID2 ID3 Full name ID4
    >> ----- --- ------------ ---------------- -----
    >> 6523 222 000000564 Adams Cody J 9999
    >> 1113 532 000000642 Barnes II Bob R 9999
    >> 786 123 000000441 Carter Sr James 9999
    >> 3333 245 000000994 Jones Jr Roy J 9999
    >> 5644 370 000000234 Martin Tom R 9999
    >> 2331 333 000000111 Van Horn Tim R 9999
    >> etc. etc....
    >>
    >> Question 1:
    >> I will be running a while(<>) loop that reads each line
    >> one at a time. How can I extract just the ID1, ID2,
    >> and the "full name" into string variables during each
    >> loop iteration???

    >
    >If it is like you show it, using spaces between columns and between
    >words in the name, you weep. Remember that some full names contain
    >numbers (consider former NY Times reporter Jennifer 8 Lee).


    Actually, it is very simple:
    - split() on space character
    - grab the first 3 items, from these throw away the third
    - and from the remaining items throw away the last item and re-combine
    the others for the name

    >I'd start something like this:
    >
    >/^(\d+)\s+(\d+)\s+\d+\s+(.*[^\s])\s+\d+$/


    You don't have to use a hammer to drive a screw. Perl's toolbox contains
    many more tools than just REs.

    jue
     
    Jürgen Exner, Aug 29, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    893
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    591
    Naren
    May 11, 2004
  3. Rainer Weikusat

    Re: String parsing (2 questions)

    Rainer Weikusat, Aug 28, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    351
    Rainer Weikusat
    Aug 28, 2012
  4. Jürgen Exner

    Re: String parsing (2 questions)

    Jürgen Exner, Aug 29, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    333
    Jürgen Exner
    Aug 29, 2012
  5. Rainer Weikusat

    Re: String parsing (2 questions)

    Rainer Weikusat, Aug 29, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    338
    Rainer Weikusat
    Aug 29, 2012
Loading...

Share This Page