At 8:07 PM -0400 6/19/10, 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

The "clever" way is to use a regular expression that covers all of the possible cases. Here is one that works for your sample string:

$x = q(Hughes, D 2-0, O'Flaherty 2-0, Moylan 1-1);
while( $x =~  m{ \s* ([\w,\s]+) \s+ (\d+-\d+) \s* (,|\z) }gx ) {
  print "$1: $2\n";
}

If not all entries have two numbers separated by a dash, then the above won't work.


--
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