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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re^2: Matching part of a line and saving it into a variable

Replies are listed 'Best First'.
Re^3: Matching part of a line and saving it into a variable
by CountZero (Bishop) on Mar 14, 2011 at 09:20 UTC
    What is so difficult about showing us --say-- 10 relevant lines of your CSV file (including the header line, if any), so we can work on it? Is the data that secret and confidential?

    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

      Sorry, I was trying as much as possible to do it myself and following tips to try and learn. Thanks for helping thus far. Here are a few lines:

      "Barber Shop: NW, 147-2220, Washington 992,,,,, ,"Lawrence, Jerry A.",Buzz,,Clean,Cash,, ,"Wallace, Bob W.",Caesar,,High,Credit,, ,"Anthony, Ronald",Caesar,,Close,Cash,, ,"Nelson, Dwight R.",Buzz,,Clean,Check,, ,"Jamison, Jeff S.",Caesar,,High,Credit,, ,"Favors, Eric S.",Shave,,Skin,Cash,, ,"Williams, Herb C.",Fade,,High,Check,, ,"Carter, Shaun ",Caesar,,Low,Cash,,

      Once again, all I want to do is save the name into $last $first $mid A few people don't have middle initials though, so I'm not entirely sure how to handle that..

        What do you think of this?
        use Modern::Perl; while (<DATA>) { my ($last, $first, $mid) = /^,"([^",]+),\s([^\s"]+)\s?([^"]*)/; say "LAST: $last FIRST: $first MID: $mid" if $last; } __DATA__ "Barber Shop: NW, 147-2220, Washington 992,,,,, ,"Lawrence, Jerry A.",Buzz,,Clean,Cash,, ,"Wallace, Bob W.",Caesar,,High,Credit,, ,"Anthony, Ronald",Caesar,,Close,Cash,, ,"Nelson, Dwight R.",Buzz,,Clean,Check,, ,"Jamison, Jeff S.",Caesar,,High,Credit,, ,"Favors, Eric S.",Shave,,Skin,Cash,, ,"Williams, Herb C.",Fade,,High,Check,, ,"Carter, Shaun ",Caesar,,Low,Cash,,
        Output:
        LAST: Lawrence FIRST: Jerry MID: A. LAST: Wallace FIRST: Bob MID: W. LAST: Anthony FIRST: Ronald MID: LAST: Nelson FIRST: Dwight MID: R. LAST: Jamison FIRST: Jeff MID: S. LAST: Favors FIRST: Eric MID: S. LAST: Williams FIRST: Herb MID: C. LAST: Carter FIRST: Shaun MID:

        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

Re^3: Matching part of a line and saving it into a variable
by fidesachates (Monk) on Mar 14, 2011 at 15:15 UTC
    The other monks have asked for data from you which is reasonable. You don't seem able to comply with such a simple request.

    Nonetheless, I will attempt to help you solve your problem based on the code you posted above and some assumptions. N.B. this solution isn't promised to work since you won't comply with what the monks have asked.

    In your code above, you posted a regular expression if (/\"(.*)\"/). If you change it to if (/\"(.*?),(.*?)\"/), it might work. Or maybe modify the split command to include commas might work.

    Sure the solution above is vague, but then again so is your data. To solve your first line issue, easiest way is to keep a count variable.
    #!/usr/local/bin/perl open (MYFILE, 'shoplist.csv'); my $i=0; while (<MYFILE>) { chomp; if($i == 0) { $i++; next; } if (/\"(.*)\"/) { my ($first,$last)=split('\s',$_); print "$last $first\n"; } } close (MYFILE);
Re^3: Matching part of a line and saving it into a variable
by fidesachates (Monk) on Mar 14, 2011 at 20:22 UTC
    Assuming the output you want is:
    Lawrence, Jerry A. Wallace, Bob W. Anthony, Ronald Nelson, Dwight R. Jamison, Jeff S. Favors, Eric S. Williams, Herb C. Carter, Shaun


    This code works
    #!/db/bin/perl -w use strict; use warnings; my $filename = $ARGV[0]; open FILE, $filename or die $!; my $i = 0; while(<FILE>) { if($i == 0) { $i++; next; } chomp; if (/\"(.*?), (.*?)[" ]((.*?)\")?/) { my $last = $1; my $first = $2; my $mi = $4; if(defined $mi) { print "$last, $first $mi\n"; } else { print "$last, $first\n"; } } } close FILE;
      That works awesomely! May I ask, what the first if clause does?

      if($1 == 0)

      I took the regex match and the printing code and replaced the code I had before and it worked marvelously, thanks so much. Now I need to play with the $last $first $mi variables and figure out how to output them into a javascript array. I might be back later on this evening unless I can figure it out.
        You stated that you didn't want to print the first line. Notice the variable $i is initialized to 0. That if clause checks if the variable $i is equal to 0. Since we initialized it to 0, it enters the clause. The first line increases the variable $i to 1. Then it tells the while loop to skip to the next iteration. Now when you come to the clause this time, it skips it and continues to the rest of the while code because variable $i now equals 1.

        By the way, glad you're asking questions. The more you ask now, the less you'll ask in the long run.
      Double quotes are not magic in a regex, so you do not have to escape them.

      If you want to skip the first line of your loop, why not simply do a <FILE> just before the loop? That reads the first line and discards it.

      You do not need a chomp inside the loop. Your regex extracts the fields well before it reaches the end of the string.

      Always use a three argument open. What do you think will happen when someone maliciously enters >myfile.txt as the filename argument? Indeed, bye bye file contents!

      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

        Good points. 3/4 of them were due to me copying the OP's code with barely a glance.