From: Wei Feng Date: 2012-12-20T08:31:51+09:00 Subject: Re: Perl to Ruby: regex captures to assignment. --047d7bfcf524baa36804d13cecc7 Content-Type: text/plain; charset=ISO-8859-1 Try this: w, n, d = frac_str =~ %r{/} \ ? frac_str.match(%r{(?:(\S+) )*?(\S+)/(\S+)}).captures \ : [1, frac_str, 0] I've use %r{} to quote regex, so no need to escape for / On Thu, Dec 20, 2012 at 8:45 AM, Derrick B. wrote: > Hello all, > > I am converting a Perl program to Ruby in order to learn Ruby. There is > an expression that takes a string, ("1 3/4" or "5", for example) > determines what kind of fraction was entered, and assigns variables (w, > n, d), appropriately. If a whole number was entered, then it assigns > the variables differently, of course. > > The expression first checks for the presence of a slash ("/") to > determine if it is a fraction or whole number. Then, using the > conditional operator, assigns the variables, appropriately. > > Here are the two expressions: > > Perl: > > my ($w, $n, $d) = ( $frac_str =~ /\// ) > ? $frac_str =~ /(?:(\S+) )??(\S+)\/(\S+)/ > : ( 1, $frac_str, 0 ); > > Ruby: > > w, n, d = frac_str.match(/\//) \ > ? frac_str.match(/(?:(\S+) )*?(\S+)\/(\S+)/).captures \ > : 0, frac_str, 1 > puts "w: #{w}, n: #{n}, d: #{d}" # check assignment > > > I used irb to test the regex and it works just fine: > > exp = Regexp.new(/(?:(\S+) )*?(\S+)\/(\S+)/) > => /(?:(\S+) )*?(\S+)\/(\S+)/ > > str = "1 3/4" > => "1 3/4" > > n, w, d = str.match(exp).captures > => ["1", "3", "4"] > > w > => "3" > > n > => "1" > > d > => "4" > > But, the above version outputs: > > w: 134, n: 1 3/4, d: 1 > > It appears to recurse over the regex and duplicate the captures. But, > since I am new to this language, I know I can use an 'if..else' > statement, but since I am coming from Perl, I figured that a similar > expression would "just work"! :) > > Thanks for your time and input, > > Derrick > > -- > Posted via http://www.ruby-forum.com/. > > -- Regards, Wei Feng 03 9005 3441 | M 0413 658 250 | windix@gmail.com | http://wei.feng.id.au --047d7bfcf524baa36804d13cecc7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Try this:

w, n, d =3D frac_str =3D~ %r{/} \
? frac_str.matc= h(%r{(?:(\S+) )*?(\S+)/(\S+)}).captures \
: [1, frac_str, 0]

I've use %r{} to quote regex, so no need to escape = for /


On= Thu, Dec 20, 2012 at 8:45 AM, Derrick B. <lists@ruby-forum.com>= wrote:
Hello all,

I am converting a Perl program to Ruby in order to learn Ruby. =A0There is<= br> an expression that takes a string, ("1 3/4" or "5", for= example)
determines what kind of fraction was entered, and assigns variables (w,
n, d), appropriately. =A0If a whole number was entered, then it assigns
the variables differently, of course.

The expression first checks for the presence of a slash ("/") to<= br> determine if it is a fraction or whole number. =A0Then, using the
conditional operator, assigns the variables, appropriately.

Here are the two expressions:

Perl:

=A0 =A0 my ($w, $n, $d) =3D ( $frac_str =3D~ /\// )
=A0 =A0 =A0 =A0 =A0 =A0 ? $frac_str =3D~ /(?:(\S+) )??(\S+)\/(\S+)/
=A0 =A0 =A0 =A0 =A0 =A0 : ( 1, $frac_str, 0 );

Ruby:

=A0 =A0 w, n, d =3D frac_str.match(/\//) \
=A0 =A0 =A0 =A0 ? frac_str.match(/(?:(\S+) )*?(\S+)\/(\S+)/).captures \
=A0 =A0 =A0 =A0 : 0, frac_str, 1
=A0 =A0 puts "w: #{w}, n: #{n}, d: #{d}" =A0# check assignment

I used irb to test the regex and it works just fine:

exp =3D Regexp.new(/(?:(\S+) )*?(\S+)\/(\S+)/)
=3D> /(?:(\S+) )*?(\S+)\/(\S+)/

str =3D "1 3/4"
=3D> "1 3/4"

n, w, d =3D str.match(exp).captures
=3D> ["1", "3", "4"]

w
=3D> "3"

n
=3D> "1"

d
=3D> "4"

But, the above version outputs:

w: 134, n: 1 3/4, d: 1

It appears to recurse over the regex and duplicate the captures. =A0But, since I am new to this language, I know I can use an 'if..else'
statement, but since I am coming from Perl, I figured that a similar
expression would "just work"! =A0:)

Thanks for your time and input,

Derrick

--
Posted via http://= www.ruby-forum.com/.




-- Regards,
Wei Feng

03 9005 3441 | M 0413 658 250 | windix@gmail.com | http://wei.feng.id.au
--047d7bfcf524baa36804d13cecc7--