On Tue, 2 Sep 2003 23:05:05 +0900
Thomas Adam <thomas_adam16 / yahoo.com> wrote:

>  --- Michael Campbell <michael_s_campbell / yahoo.com> wrote: > (I think...)
> > #2
> > 
> > Is there a way to get matched data into variables without using
> > matchdata, or perl ugly-variables?
> > 
> > Again, in perl I could do something like:
> > 
> > $_ = "Dec 12";
> > (mon, day) = /(\w+) (\d+)/;
> 
> this = "Dec 12"
> that = $1 if this =~ /(\w+) (\d+)/
> other = $2 if this =~ /(\w+) (\d+)/

MatchData has a #captures method as of 1.8,so you could also do
something like:

this = "Dec 12"
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

....but this will run into problems when there's not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
  that, other = m.captures
  # Do stuff with "that" and "other"
else
  # We didn't match, do something about it
end

Also, if I was going to do that way you listed, I'd say it like:

if this =~ /(\w+) (\d+)
  that, other = $1, $2
end

or even:

that, other = $1, $2 if this =~ /(\w+) (\d+)

but this still runs into the problem of when there's no match.

Jason Creighton