Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: Simple question(s
Thomas Adam
9/4/2003 8:50:00 AM
--- Jason Creighton <androflux@softhome.net> wrote:
> 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
That's right, Jason -- I couldn't for the life of me remember what it was
:) Incidentally, small world, is it not :)
> ....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
I think we can make the assumption in this case that there will be a
match.
> 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
Each to their own.
-- Thomas Adam
=====
Thomas Adam
"The Linux Weekend Mechanic" -- www.linuxgazette.com
________________________________________________________________________
Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger
http://mail.messenger.y...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Simple question(s
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password