Regexps handle only the first number

Regexps handle only the first number

am 07.09.2009 20:41:47 von Mishustin Alexey

Hello,

Why do these regexps handle only the first number?

$ cat ./test.pl
#!/usr/bin/perl

use strict;
use warnings;

my $raised_num = 'Tab_10' =~ /^Tab_(.*)$/;
print $raised_num."\n";

$raised_num = 'Tab_11' =~ /^Tab_([0-9]+)$/;
print $raised_num."\n";

$raised_num = 'Tab_12' =~ /^Tab_([0-9][0-9])$/;
print $raised_num."\n";
$ ./test.pl
1
1
1

But, according to all rules, both numbers must be handled:

10
11
12

--
Regards,
Alex


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 20:47:33 von Uri Guttman

>>>>> "MA" == Mishustin Alexey writes:

MA> Why do these regexps handle only the first number?


MA> my $raised_num = 'Tab_10' =~ /^Tab_(.*)$/;
MA> print $raised_num."\n";

MA> $raised_num = 'Tab_11' =~ /^Tab_([0-9]+)$/;
MA> print $raised_num."\n";

MA> $raised_num = 'Tab_12' =~ /^Tab_([0-9][0-9])$/;
MA> print $raised_num."\n";

MA> $ ./test.pl
MA> 1
MA> 1
MA> 1

try changing the number to something that doesn't start with 1 and see
the results. then come back here for why this is happening.

MA> But, according to all rules, both numbers must be handled:

MA> 10
MA> 11
MA> 12

you aren't obeying the rules! look carefully at the docs about the
return value of // and how to grab things. my clue above should help.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:01:12 von Mishustin Alexey

В Пнд, 07/09/2009 в 14:47 -0400, Uri Guttman =D0=
¿Ð¸ÑˆÐµÑ=82:
> >>>>> "MA" == Mishustin Alexey writes:
>=20
> MA> Why do these regexps handle only the first number?
>=20
>=20
> MA> my $raised_num =3D 'Tab_10' =3D~ /^Tab_(.*)$/;
> MA> print $raised_num."\n";
>=20
> MA> $raised_num =3D 'Tab_11' =3D~ /^Tab_([0-9]+)$/;
> MA> print $raised_num."\n";
>=20
> MA> $raised_num =3D 'Tab_12' =3D~ /^Tab_([0-9][0-9])$/;
> MA> print $raised_num."\n";
>=20
> MA> $ ./test.pl=20
> MA> 1
> MA> 1
> MA> 1
>=20
> try changing the number to something that doesn't start with 1 and see
> the results. then come back here for why this is happening.
>=20
> MA> But, according to all rules, both numbers must be handled:
>=20
> MA> 10
> MA> 11
> MA> 12
>=20
> you aren't obeying the rules! look carefully at the docs about the
> return value of // and how to grab things. my clue above should help.

Unreal...:

$ cat ./test.pl=20
#!/usr/bin/perl

use strict;
use warnings;

my $raised_num =3D 'Tab_22' =3D~ /^Tab_(.*)$/;
print $raised_num."\n";

$raised_num =3D 'Tab_23' =3D~ /^Tab_([0-9]+)$/;
print $raised_num."\n";

$raised_num =3D 'Tab_24' =3D~ /^Tab_([0-9][0-9])$/;
print $raised_num."\n";
$ ./test.pl=20
1
1
1

I give up!

What's the mystery?

--=20
Regards,
Alex


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:03:43 von Mishustin Alexey

В Пнд, 07/09/2009 в 23:01 +0400, Mishustin Alexey =
пишет:

> I give up!
>=20
> What's the mystery?

Aaaa,

($raised_num) =3D 'Tab_22' =3D~ /^Tab_(.*)$/;
print $raised_num."\n";

($raised_num) =3D 'Tab_23' =3D~ /^Tab_([0-9]+)$/;
print $raised_num."\n";

($raised_num) =3D 'Tab_24' =3D~ /^Tab_([0-9][0-9])$/;
print $raised_num."\n";

!!

Thanx!

--=20
Regards,
Alex


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:09:09 von Shawn H Corey

Mishustin Alexey wrote:
> Aaaa,
>
> ($raised_num) = 'Tab_22' =~ /^Tab_(.*)$/;
> print $raised_num."\n";

Not quite right. Try:
( $raised_num = 'Tab_22' ) =~ /^Tab_(.*)$/;


--
Just my 0.00000002 million dollars worth,
Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:12:49 von Uri Guttman

>>>>> "MA" == Mishustin Alexey writes:

MA> В Пнд, 07/09/2009 в 23:01 +0400, Mishustin A=
lexey пишет:
>> I give up!
>>=20
>> What's the mystery?

MA> Aaaa,

MA> ($raised_num) =3D 'Tab_22' =3D~ /^Tab_(.*)$/;
MA> print $raised_num."\n";

MA> ($raised_num) =3D 'Tab_23' =3D~ /^Tab_([0-9]+)$/;
MA> print $raised_num."\n";

MA> ($raised_num) =3D 'Tab_24' =3D~ /^Tab_([0-9][0-9])$/;
MA> print $raised_num."\n";

MA> !!

see? wasn't it more fun and educational to discover the answer without
being given it directly? :)

uri

--=20
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:14:07 von Uri Guttman

>>>>> "SHC" == Shawn H Corey writes:

SHC> Mishustin Alexey wrote:
>> Aaaa,
>>
>> ($raised_num) = 'Tab_22' =~ /^Tab_(.*)$/;
>> print $raised_num."\n";

SHC> Not quite right. Try:
SHC> ( $raised_num = 'Tab_22' ) =~ /^Tab_(.*)$/;

that does nither more than assign 'Tab_22' to the var and then grab the
number into $1. he wants the grabbed number in the variable.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:17:50 von Mishustin Alexey

В Пнд, 07/09/2009 в 15:12 -0400, Uri Guttman =D0=
¿Ð¸ÑˆÐµÑ=82:

> see? wasn't it more fun and educational to discover the answer without
> being given it directly? :)

Sure! :)

--=20
Regards,
Alex


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:18:00 von Mishustin Alexey

В Пнд, 07/09/2009 в 15:09 -0400, Shawn H Corey =D0=
¿Ð¸ÑˆÐµÑ=82:

> > ($raised_num) =3D 'Tab_22' =3D~ /^Tab_(.*)$/;
> > print $raised_num."\n";
>=20
> Not quite right. Try:
> ( $raised_num =3D 'Tab_22' ) =3D~ /^Tab_(.*)$/;

It's not what I need:

$ cat ./test.pl=20
#!/usr/bin/perl

use strict;
use warnings;

my $raised_num;

($raised_num =3D 'Tab_22') =3D~ /^Tab_(.*)$/;
print $raised_num."\n";

($raised_num =3D 'Tab_23') =3D~ /^Tab_([0-9]+)$/;
print $raised_num."\n";

($raised_num =3D 'Tab_24') =3D~ /^Tab_([0-9][0-9])$/;
print $raised_num."\n";
$ ./test.pl=20
Tab_22
Tab_23
Tab_24

--=20
Regards,
Alex


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 07.09.2009 21:48:48 von Shawn H Corey

Mishustin Alexey wrote:
> It's not what I need:

Oops, you're right. I should pay attention to what I read.


--
Just my 0.00000002 million dollars worth,
Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Regexps handle only the first number

am 08.09.2009 06:40:45 von Tim Bowden

On Mon, 2009-09-07 at 15:12 -0400, Uri Guttman wrote:
> >>>>> "MA" == Mishustin Alexey writes:
>
> MA> В Пнд, 07/09/2009 в 23:01 +0400, Mishustin Alexey пишет:
> >> I give up!
> >>
> >> What's the mystery?
>
> MA> Aaaa,
>
> MA> ($raised_num) = 'Tab_22' =~ /^Tab_(.*)$/;
> MA> print $raised_num."\n";
>
> MA> ($raised_num) = 'Tab_23' =~ /^Tab_([0-9]+)$/;
> MA> print $raised_num."\n";
>
> MA> ($raised_num) = 'Tab_24' =~ /^Tab_([0-9][0-9])$/;
> MA> print $raised_num."\n";
>
> MA> !!
>
> see? wasn't it more fun and educational to discover the answer without
> being given it directly? :)
>
> uri

I like reading these little problems. MA wasn't the only one who
learned something here. I hadn't thought of evaluating regexs in list
context (says me wondering how many times I've done $a = $1; $b =
$2;...)

Great stuff. Not sure if the list is a supplement to the books
(Learning Perl etc) or the books a supplement to the list. Both good.

Tim Bowden


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/