in reply to Matching part of a line and saving it into a variable

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

Replies are listed 'Best First'.
Re^2: Matching part of a line and saving it into a variable
by mistamutt (Novice) on Mar 14, 2011 at 08:03 UTC
    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