[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01873] Re: Yet ANOTHER Perl2Ruby Question

From: Dave Thomas <Dave@...>
Date: 2000-03-15 22:30:22 UTC
List: ruby-talk #1873
"David Douthitt" <DDouthitt@cuna.com> writes:

> How do you translate this snippet:
> 
> ($var1, $var2, $var3) = /pattern (sub1) (sub2) (sub3)/;
> 
> I tried this:
> 
> var1 = (line =~ /pattern (sub1) (sub2) (sub3)/)
> 
> and it was syntactically successful but the contents of var1 were
> unexpected (zero).

A regular expression match returns the offset of the start of the
match. You can get the individual submatches by indexing the special
variable $~, or by using the Regexp.match method

   if line =~ /asdasd (\d+) (\d+) (\d+)/
      v1, v2, v3 = $~[1..-1]
   end

or

   v1, v2, v3 = /asasd (\d+) (\d+) (\d+)/.match(line)[1..-1]

However this latter form will fail if the match fails.

Regards


Dave

In This Thread

Prev Next