Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > greediness problem?

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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/
 
Reply With Quote
 
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

 
Reply With Quote
 
Tad McClellan
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..?



By fixing your code.

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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
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) {
....
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-greediness in a regex - need some help verifying syntax Wes Gamble Ruby 5 08-04-2006 03:07 PM
local greediness ??? tygerc@gmail.com Python 3 04-19-2006 01:47 PM
Regexp greediness. adamomitcheney@kiwis.co.uk Perl Misc 50 02-25-2006 02:11 AM
Kodak's LS443 Camera *or* Kodak's Greediness at its Worst enri Digital Photography 72 10-19-2005 09:44 PM
Kodak's LS443 Camera *or* Kodak's Greediness at its Worst enri Digital Photography 0 10-03-2005 02:38 PM



Advertisments