On Sat, Jun 19, 2010 at 20:07,  <stu21...@lycos.com> wrote:
> I have some text that specifies inherited runners in baseball:
>
>  'Hughes, D 2-0, O'Flaherty 2-0, Moylan 1-1'
>
> I want to split on the comma and associate the numbers with that player.  The
> problem is that sometimes the player's first initial is used sometimes not.
> Is there a clever way to consider the comma part of the name when an initial
> is used and a delimiter otherwise?  Thanx
snip

Given the format of your data I would use a regex instead of a split:

#!/usr/bin/perl

use strict;
use warnings;

my $s = "Hughes, D 2-0, O'Flaherty 2-0, Moylan 1-1";

my @runners = $s =~ /(.*?[0-9]+-[0-9]+)(?:, |$)/g;

print "I saw these runners:\n", map { "\t$_\n" } @runners;


Here is the regex broken down:

my @runners = $s =~ /   #match
        (               #start capture
                .*?     #the shortest string that is followed by
                [0-9]+  #any positive integer followed by
                -       #a hyphen followed by
                [0-9]+  #any positive integer followed by
        )               #end capture
        (?:             #start group
                ,[ ]    #a comma followed by a space
                |       #or
                $       #the end of the string
        )               #end group
/gx;



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to