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

Hello. I'm having a problem saving part of a line from a CSV file into a variable. I know that using the CSV parser would make it easy, but I'm not allowed to use that parser in this assignment. When I look at the csv file in a browser the part I'm trying to save into variables is the name of a person. The name comes out like so:

"Last, First Initial."
ex: "Smith, John A."

I've tried referencing this O'Reilly book with no success. Could anyone help me save the name of the person?
  • Comment on Matching part of a line and saving it into a variable

Replies are listed 'Best First'.
Re: Matching part of a line and saving it into a variable
by GrandFather (Saint) on Mar 14, 2011 at 05:12 UTC

    But have you tried writing any code? If so, perhaps you could share it with us so we can help you solve the problem. Maybe the following template will help:

    use strict; use warnings; while (<DATA>) { ... print "Last: $last, First: $first, Initial: $initial\n"; } __DATA__ "Smith, John A.", 23, Red

    You may wish to edit the sample data of course.

    True laziness is hard work
      Using what you said as a template, I've come up with this so far:
      #!/usr/local/bin/perl open (MYFILE, 'shoplist.csv'); while (<MYFILE>) { chomp; if (/\"(.*)\"/) { my ($first,$last) = split('\s', $_); + print "$last $first\n"; } } close (MYFILE);

      It's working decently, how do you get rid of commas and a double quote in a line? For example, the names are coming out like:

      Last, "First

      I'd like to remove the comma and the double quote so I can just have the last and first name. I'm getting pretty close, all that's left is to figure out how to save the middle initial!

        My template used strictures (use strict; use warnings;), didn't require an external file, and provided some sample data. You didn't use my template!

        Unless you show us a few lines of sample data, the output you expect and the output you see there is not much we can do to help. We are at least as lazy as you are so if you want our help you are going to have to work hard to make it easy for us. After all, I worked hard to provide you a template in the hope that I wouldn't have to write all this lot. Looks like I failed!

        True laziness is hard work
Re: Matching part of a line and saving it into a variable
by CountZero (Bishop) on Mar 14, 2011 at 07:28 UTC
    You will have to give us some examples of the lines in your CSV file to enable the Monks to work on it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    A reply falls below the community's threshold of quality. You may see it by logging in.