--- "Randy W. Sims" <[EMAIL PROTECTED]>
wrote:
> 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?
>
Wow, yeah that helps a lot.
Here's a question: If if had:
$line = 'Spurs 94, Suns 82, Heat 99, Magic 74'
and then did a split on comma and comma's surrounding
spaces:
@result = split (/\s*,\s*/, $line);
then @result would look like this, right?
@result[0] = 'Spurs 94'
@result[1] = 'Suns 82'
@result[2] = 'Heat 99'
@result[3] = 'Magic 74'
If I wanted to split on the numbers as well, why
doesn't this work:
@result = split (/\s*\d*,\s*\d*/, $line);
I just had a thought, it have to look more like:
@result = split (/(\s*|\d*),\s*\d*/, $line);
I'm confusing myself, but when I get home, I'll try
out what you've shown me. That might be the way to do
it.
> Randy.
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>