Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi i'm trying to compare 2 strings one is just a word and the other is a dictionary entry (word and phonetic transcription), when i try to compare 'you' it matches 'you' & 'you'll' and 'you're' etc, i know i could get rid of ' but i also need to match 'you'd'. at the mo i'm using if ($short1 eq $short2). thanks

Replies are listed 'Best First'.
Re: V basic question about eq
by sauoq (Abbot) on Nov 21, 2003 at 17:32 UTC

    when i try to compare 'you' it matches 'you' & 'you'll' and 'you're' etc
    ...
    i'm using if ($short1 eq $short2).

    Okay, you've confused me. The string 'you' does not "match" the string "you'll" using string equality (eq).

    All I got out of your post is that you are looking for a way to find strings that contain 'you' might contain "you'd" but must not contain "you'll" or "you're"... is that right? Something like

    $short1 =~ /^you(?:'d)?$/;
    perhaps?

    If not, maybe you could supply us with some code or a better example.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Yeah, that's what threw me. He used single quotes though, so I figured the apostrophy was seen as the end of the string. Don't know if that's possible because I'm a beginner too, but that's how it sounds.
      sauoq sorry i didn't make myself clear, i want to match 2 strings exactly but when i use eq it matches you & you'll & you've etc. however i don't want to disregard the ' altogether as i may also have to match you'll at a different time. hope this makes more sense thanks
        regarding my regex:
        it's still not correct.say for american it's matching and outputting american AND american's. i want to disregard the second occurrence unless it matches the string completely?
        regarding eq:
        when i use eq it matches you & you'll & you've
        Are those your theories, or have you actually tested the code and seen that behavior?
Re: V basic question about eq
by Wassercrats (Initiate) on Nov 21, 2003 at 17:07 UTC
    I'd use a regular expression and escape the apostrophies with \Q, such as

    if ($short1 =~ /\Q$short2\E/)

      and escape the apostrophies with \Q

      Using \Q is a good idea in any case, but apostrophes aren't regular expression metacharacters.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: V basic question about eq
by Roy Johnson (Monsignor) on Nov 21, 2003 at 17:28 UTC
    If you just want to see that they start the same, you could do if ($short1 eq substr($short2, 0, length($short1))).
Re: V basic question about eq
by Wassercrats (Initiate) on Nov 21, 2003 at 17:10 UTC
    Better make that

    if ($short1 =~ /^\Q$short2\E$/)

      thanks for that but it's still not correct.say for american it's matching and outputting american AND american's. i want to disregard the second occurance unless it matches the string completely???????? anyway it's friday nite, i'll deal with it on monday cheers