Velocity Reviews > Perl > greediness problem?

# greediness problem?

justme
Guest
Posts: n/a

 02-07-2005
hi

i have an initial string such as
\$initial = "qqqqqqqq";
a user needs to input another string
\$string =<STDIN>;
If this user were to key in exactly 8 q's then it will match and do
something.
But i have a problem when the user key in 5 or 6 q's, it will still
match, which i do not want.
how can i overcome this problem..?
thanks

Josef Moellers
Guest
Posts: n/a

 02-07-2005
justme wrote:
> hi
>
> i have an initial string such as
> \$initial = "qqqqqqqq";
> a user needs to input another string
> \$string =<STDIN>;
> If this user were to key in exactly 8 q's then it will match and do
> something.
> But i have a problem when the user key in 5 or 6 q's, it will still
> match, which i do not want.
> how can i overcome this problem..?

How did you attempt to solve this in the first case?
the re /q{8}/ would match exactly 8 q's, not 5 nor 6 nor 7, exactly 8,
nor does it match 9, 10, 11 or any above that, exactly 8 which is the
holy number (thanks to Monty Python),

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Rasto Levrinc
Guest
Posts: n/a

 02-07-2005
Josef Moellers wrote:

>
> How did you attempt to solve this in the first case?
> the re /q{8}/ would match exactly 8 q's, not 5 nor 6 nor 7, exactly 8,
> nor does it match 9, 10, 11 or any above that, exactly 8 which is the
> holy number (thanks to Monty Python),
>
> Josef

Actually /q{8}/ would match 9, 10, 11 q's.
/^q{8}\$/ would work better.

Rasto

--

Rasto Levrinc
http://sourceforge.net/projects/rlocate/

Josef Moellers
Guest
Posts: n/a

 02-07-2005
Rasto Levrinc wrote:
> Josef Moellers wrote:
>
>>
>> How did you attempt to solve this in the first case?
>> the re /q{8}/ would match exactly 8 q's, not 5 nor 6 nor 7, exactly 8,
>> nor does it match 9, 10, 11 or any above that, exactly 8 which is the
>> holy number (thanks to Monty Python),
>>
>> Josef

>
>
> Actually /q{8}/ would match 9, 10, 11 q's.
> /^q{8}\$/ would work better.

Hm ... nitpicking: q{8} matches 8 q's, leaving the remaining q's unmatched.
But ... you're absolutely right: the intended message that q{8} would
match _exactly_ 8 q's and fail on 9 or more is wrong.

Thanks,

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Guest
Posts: n/a

 02-07-2005
justme <(E-Mail Removed)> wrote:

> i have an initial string such as
> \$initial = "qqqqqqqq";
> a user needs to input another string
> \$string =<STDIN>;
> If this user were to key in exactly 8 q's then it will match and do

^^^^^^^^^^^^^
> something.

Perl has an operator for testing string equality, its name is "eq".

> But i have a problem when the user key in 5 or 6 q's, it will still
> match, which i do not want.

Then there must be something wrong with your code.

> how can i overcome this problem..?

--
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas

Tintin
Guest
Posts: n/a

 02-07-2005

"justme" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> hi
>
> i have an initial string such as
> \$initial = "qqqqqqqq";
> a user needs to input another string
> \$string =<STDIN>;
> If this user were to key in exactly 8 q's then it will match and do
> something.
> But i have a problem when the user key in 5 or 6 q's, it will still
> match, which i do not want.
> how can i overcome this problem..?

Don't use a regex (although you haven't showed us what you actually tried)

Assuming you did a

chomp \$string;

then you can do

if (\$initial eq \$string) {
....
}