From: RichardOnRails Date: 2007-11-22T10:10:07+09:00 Subject: Re: False positives in editing data On Nov 21, 1:53 pm, Alex Young wrote: > RichardOnRails wrote: > > On Nov 19, 2:23 am, Ryan Davis wrote: > > >> Those evals are TERRIFYING. Don't use them. > > > How else can you loop through $1, $2 ... without repetitive code? > > I don't think anyone has replied to this, so... > > This is what you've got: > > > sName =~ /^([\d]+)?\.?([\d]+)?\.?([\d]+)?\.?/ > > > # Save match variables in n[1] ... n[MLN+1] > > n = Array.new > > (1..MLN+1).each { |i| eval %{n[i] = $#{i}} } > > You can get an equivalent for n with the String#match method: > > irb(main):001:0> name = "2.2.02Topic 2.2.2" > => "2.2.02Topic 2.2.2" > irb(main):002:0> m = name.match( /^([\d]+)?\.?([\d]+)?\.?([\d]+)?\.?/ ) > => # > irb(main):003:0> m[1] > => "2" > irb(main):004:0> m[2] > => "2" > irb(main):005:0> m[3] > => "02" > > You should be able to use that to get around your second eval, too. > > -- > Alex Hi Alex, > m = name.match( /^([\d]+)?\.?([\d]+)?\.?([\d]+)?\.?/ ) Thanks. Best wishes, Richard