greediness problem?

J

justme

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
 
J

Josef Moellers

justme said:
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
 
R

Rasto Levrinc

Josef said:
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
 
J

Josef Moellers

Rasto said:
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
 
T

Tad McClellan

justme said:
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..?


By fixing your code.

We cannot help you fix your code because we cannot see your code...
 
T

Tintin

justme said:
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) {
....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top