greediness problem?

Discussion in 'Perl Misc' started by justme, Feb 7, 2005.

  1. justme

    justme Guest

    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
    justme, Feb 7, 2005
    #1
    1. Advertising

  2. 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
    Josef Moellers, Feb 7, 2005
    #2
    1. Advertising

  3. 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/
    Rasto Levrinc, Feb 7, 2005
    #3
  4. 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
    Josef Moellers, Feb 7, 2005
    #4
  5. justme <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 7, 2005
    #5
  6. justme

    Tintin Guest

    "justme" <> wrote in message
    news:...
    > 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) {
    ....
    }
    Tintin, Feb 7, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    3,953
    Andreas Suurkuusk
    Jul 27, 2003
  2. local greediness ???

    , Apr 19, 2006, in forum: Python
    Replies:
    3
    Views:
    302
    Paul McGuire
    Apr 19, 2006
  3. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    535
    Bullschmidt
    May 11, 2004
  4. Wes Gamble
    Replies:
    5
    Views:
    91
    Wes Gamble
    Aug 4, 2006
  5. Regexp greediness.

    , Feb 14, 2006, in forum: Perl Misc
    Replies:
    50
    Views:
    382
    robic0
    Feb 25, 2006
Loading...

Share This Page