Re: String parsing (2 questions)

Discussion in 'Perl Misc' started by Rainer Weikusat, Aug 28, 2012.

  1. "Robert Crandal" <> writes:
    > 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???


    Without knowing more about the line format, this can't really be
    answered. Untested suggestion:

    if (/^([0-9]+)\s+([0-9]+)\s+[0-9]+\+s([A-Za-z].*)\s+[0-9]/) {
    $id1 = $1;
    $id2 = $2;
    $full_name = $3;
    } else {
    # complain about wrong format?
    next;
    }

    > Question 2: (difficult)
    > Suppose we have extracted the "full name" string into
    > a variable named $full_name. How can I extract
    > the "first name", "middle initial", and "last name plus suffix"?


    Split the string on whitespace. Complain if there aren't at least two
    elements in the resulting array. Set 'first name' to element #0. If
    there are exactly two elements, set last name to element #1. If there
    are more than two and the seconds is a capital letter followed by a
    dot, assume its a middle initial and use everything behind it as last
    name. Otherwise, use everything behind the first name.

    Example code for that (parse the first commandline argument):

    ---------------
    @name = split(/\s+/, $ARGV[0]);

    die("must have at least two parts") unless @name > 1;

    $first = shift(@name);

    $middle = shift(@name)
    if @name > 1 && $name[0] =~/^[A-Z]\.$/;

    print('first ', $first, "\n", 'middle ', $middle, "\n",
    'last ', join(' ', @name), "\n");
    Rainer Weikusat, Aug 28, 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:
    873
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    579
    Naren
    May 11, 2004
  3. Jürgen Exner

    Re: String parsing (2 questions)

    Jürgen Exner, Aug 29, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    322
    Jürgen Exner
    Aug 29, 2012
  4. Jürgen Exner

    Re: String parsing (2 questions)

    Jürgen Exner, Aug 29, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    366
    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:
    331
    Rainer Weikusat
    Aug 29, 2012
Loading...

Share This Page