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

Greetings,

I'm trying to figure out how to compare a value to certain characters within a word, using an if statement.

For example, let's say I have this:

$word = "bummer";

I want to check to see if the value of "mme" is == to the 3rd, 4th, and 5th characters of $word

I don't want to just grep "mme" out of a word (that would be easy). I want to specifically compare a value to the 3rd, 4th, and 5th characters of a word.

So that,

$word = "bummer";

$match = "mme";

if (insert code here == $match)

{

print "Gotta match\n";

}

else {print "No match\n";}

How do I go about doing that?

Thanks for your help!

Replies are listed 'Best First'.
Re: Character matching in a word?
by BrowserUk (Patriarch) on Sep 21, 2004 at 17:16 UTC
    if( substr( $word, 2, 3 ) eq $match ) { ...

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Thanks for that!
Re: Character matching in a word?
by TedPride (Priest) on Sep 21, 2004 at 20:20 UTC
    Or, to make things slightly simpler:
    if (substr($word, 2, length($match)) eq $match) { print "Gotta match\n"; } else { print "No match\n"; }
    That way $match can be any length you want.
Re: Character matching in a word?
by graff (Chancellor) on Sep 22, 2004 at 03:25 UTC
    Hmm. This might do it as well:
    my $match = "mme"; my $word = "bummer"; my $seek_pos = 2; my $result; # one way: $result = ( index( $word, $match, $seek_pos ) == 0 ) ? "Got a match" : "Nada"; # another way: $result = ( index( $word, $match ) == $seek_pos ) ? "Got a match" : "Nada"; print "$result\n";