On 03/12/04 08:18, Stuart White wrote:
I like the idea of using split() but decided to keep
most of my regex and incorporate split on the string.
So the string: 'Spurs 94, Suns 82' <-and there may or
may not be a space after the 2.
I decided to read up on split(), and then try to split
it.

The split function consumes (throws away) the part of the string that matches the regular expression used as the first argument, so that it no longer appears in the result. For example, let's say that


$line = 'Spurs 94, Suns 82';

if we use split like

@result = split /,/, $line;

I.e. if we split $line on comma, then result will contain:

$result[0] = 'Spurs 94'
$result[1] = ' Suns 82'

Notice that the combination of the two elements would produce the original string without the comma. In particular, notice that a space remains in front of the second element. If we want to remove spaces on either side of the comma, we can add it to the regex like:

@result = split /\s*,\s*/, $line;

which will give us

$result[0] = 'Spurs 94'
$result[1] = 'Suns 82'

Now we can take each of the results and split on a space (or any number of spaces) to process each teams score:

foreach my $teamscore (@result) {
  my ($team,$score) = split /\s+/, $teamscore;
  print "Team: $team, Score: $score\n";
}

This would take each of the two elements in @result from above in turn and split on one-or-more-spaces, so that the first iteration of the loop would print:

Team: Spurs, Score: 94

and the second iteration will produce:

Team: Suns, Score: 82

Does that help clarify the way split works?

Randy.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to