how to write a range of 10 to 80 as a regular expresssion

how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 08:38:04 von eventual

--0-1577410954-1308638284=:43556
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,=0ALooking at the script below, how do I write a range of 10 to 80 as a=
=A0regular expression. =0AI tried [10 - 80] but it wont work.=0AThanks=0A##=
### script ##############  =0A#!/usr/bin/perl my $player_total_poi=
nts =3D 70;=0Aif ( $player_total_points =3D~/^(10..80)$/ ){     pri=
nt "yes";=0A}else{     print "no";=0A}
--0-1577410954-1308638284=:43556--

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 08:58:49 von Dean Du

--001517477f8651e2a004a6336287
Content-Type: text/plain; charset=UTF-8

$player_total_points =~/^[1-8]\d$/

2011/6/21 eventual

> Hi,
> Looking at the script below, how do I write a range of 10 to 80 as
> a regular expression.
> I tried [10 - 80] but it wont work.
> Thanks
> ##### script ##############
>
> #!/usr/bin/perl
>
> my $player_total_points = 70;
> if ( $player_total_points =~/^(10..80)$/ ){
> print "yes";
> }else{
> print "no";
> }

--001517477f8651e2a004a6336287--

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 09:06:23 von jorgw

2011/6/21 eventual :
> Hi,
> Looking at the script below, how do I write a range of 10 to 80 as a=C2=
=A0regular expression.
> I tried [10 - 80] but it wont work.


$ perl -le '$x=3D70; print "true" if grep {/^$x$/} 10 .. 80'
true

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

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 09:10:39 von Dean Du

--001517477f86999b4b04a6338c63
Content-Type: text/plain; charset=UTF-8

revised to exclude matching number greater than 80
$player_total_points =~/^[1-7]\d$/ || $player_total_points =~/^80$/

2011/6/21 Dean Du

> $player_total_points =~/^[1-8]\d$/
>
>
> 2011/6/21 eventual
>
>> Hi,
>> Looking at the script below, how do I write a range of 10 to 80 as
>> a regular expression.
>> I tried [10 - 80] but it wont work.
>> Thanks
>> ##### script ##############
>>
>> #!/usr/bin/perl
>>
>> my $player_total_points = 70;
>> if ( $player_total_points =~/^(10..80)$/ ){
>> print "yes";
>> }else{
>> print "no";
>> }
>
>
>

--001517477f86999b4b04a6338c63--

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 09:52:14 von jwkrahn

eventual wrote:
> Hi,

Hello,

> Looking at the script below, how do I write a range of 10 to 80 as a regular expression.
> I tried [10 - 80] but it wont work.
> Thanks
> ##### script ##############
>
> #!/usr/bin/perl
>
> my $player_total_points = 70;
> if ( $player_total_points =~/^(10..80)$/ ){
> print "yes";
> }else{
> print "no";
> }

Why not just do it like this:

if ( $player_total_points >= 10 && $player_total_points <= 80 ) {

But as a regular expression it would be something like this:

if ( $player_total_points =~ /\A(?:[1-7][0-9]|80)\z/ ) {




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

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

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 10:30:43 von Shlomi Fish

Hi eventual,

On Mon, 20 Jun 2011 23:38:04 -0700 (PDT)
eventual wrote:

> Hi,
> Looking at the script below, how do I write a range of 10 to 80 as a=C2=
=A0regular
> expression. I tried [10 - 80] but it wont work.
> Thanks
> ##### script ##############
>  
> #!/usr/bin/perl
>=20
> my $player_total_points =3D 70;
> if ( $player_total_points =3D~/^(10..80)$/ ){

Use:

if ($player_total_points =3D~ /\A(?:[1-7][0-9])|80\z/)

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

The X in XSLT stands for eXtermination.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 10:41:14 von Shlomi Fish

Hi Jorg,

a few comments on your code:

On Tue, 21 Jun 2011 15:06:23 +0800
"Jorg W." wrote:

> 2011/6/21 eventual :
> > Hi,
> > Looking at the script below, how do I write a range of 10 to 80 as
> > a regular expression. I tried [10 - 80] but it wont work.
>=20
>=20
> $ perl -le '$x=3D70; print "true" if grep {/^$x$/} 10 .. 80'

1. Scanning every element of a range like that would be extremely inefficie=
nt
(O(N) instead of O(1)).

2. /^$x$/ would be equivalent to $x eq $_ in this case.

3. Better use List::MoreUtils::any here when you want to convey if it
matches the presence in the list, instead of perldoc -f grep (or
perl-5.10.x-and-above's smart-match operator).

4. You should generally interpolate variables into regular expressions usin=
g \Q
and \E . See http://perldoc.perl.org/functions/quotemeta.html .

5. I prefer to use \A for start-of-string and \z for end-of-string instead =
of
"^" and "$", which are more ambiguous.

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

Real programmers donâ€=99t write workarounds. They tell their users to =
upgrade
their software.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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

Re: how to write a range of 10 to 80 as a regular expresssion

am 21.06.2011 13:15:36 von Shawn H Corey

On 11-06-21 03:52 AM, John W. Krahn wrote:
> Why not just do it like this:
>
> if ( $player_total_points >= 10 && $player_total_points <= 80 ) {

Agreed. Although Perl can switch between strings and numbers on the
fly, often it's better to treat numbers as numbers.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

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

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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