[#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:02256] Multiple assignment of pattern match results.

From: schneik@...
Date: 2000-03-30 06:18:27 UTC
List: ruby-talk #2256

Hi,

Is there a somewhat more elegant and efficient way to do multiple
assignment of pattern match results (than that shown below) that is still a
fairly simple one-liner (i.e. without resorting to auxiliary $n assignments
or to defining a wrapper function)?

# irb.rb
irb(main):001:0> x="aabbbccccdddeeeefffabcdeabcdeabcde"
"aabbbccccdddeeeefffabcdeabcdeabcde"
irb(main):002:0> (t, u, v) = x.scan(/(a+)[^ace]*(c+)[^e]*(e+)/)[0]
["aa", "cccc", "eeee"]

The only problem I have with this example is that scan is going through the
work of producing an array of arrays, even though I only want the first
match:

irb(main):003:0> (t, u, v) = x.scan(/(a+)[^ace]*(c+)[^e]*(e+)/)
[["aa", "cccc", "eeee"], ["a", "c", "e"], ["a", "c", "e"], ["a", "c", "e"]]

Since the cases where I have used analogous Perl idioms in the past tend to
be found in inner loops or in special cases, involve file-length strings,
I'd like to avoid the extra overhead of scan.

And if there is such a technique, I'd also like to be able to us it in an
if condition.

Conrad Schneiker
(This note is unofficial and subject to improvement without notice.)


In This Thread

Prev Next