On 2002.05.19, Mike Stok <mike / stok.co.uk> wrote:
> In article <20020518152610.GJ9684 / panoptic.com>, Dossy wrote:
> 
> > No, the Perlish idiom is:
> > 
> >   ($x, $y) = ("foo bar" =~ /(foo) (bar)/);
> 
> But in perl you should check that the array assignment happened e.g.
> 
>   if (my ($x, $y) = "foo bar" =~ /(foo) (bar)/) {
>      ...
>   }

Or, I just test to see if defined($x) or defined($y) is true.  If
it is, then I know the match succeeded.  If they're undef, then
the match failed.

> if you want to use $x and $y "safely", and that's not much more concise
> than the Ruby.

Ideally, in Ruby:

  x, y = "foo bar".match /(foo) (bar)/
  x         # => "foo"
  y         # => "bar"

  x, y = "foo bar".match /(foo)x(bar)/
  x         # => nil
  y         # => nil

According to David Black about Ruby 1.7, String#match returns an
array not only with the matched strings, but it basically returns
$~, so you'd have to do:

  junk, x, y = "foo bar".match /(foo) (bar)/

Which is annoying, but will suffice.

-- Dossy

-- 
Dossy Shiobara                       mail: dossy / panoptic.com 
Panoptic Computer Network             web: http://www.panoptic.com/ 
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)