Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Substring comparisson problem

by ChTidio (Initiate)
on Nov 27, 2013 at 19:09 UTC ( #1064688=perlquestion: print w/replies, xml ) Need Help??

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

A newbie in perl and I have a simple "learning" question that is driving me crazy. Using this simple snippet of code, I expected the "print" statement to execute, but it never does. What am I doing wrong?
my $string = "Hello World"; if ($string =~ /"Hello"/) { print "Basic String contains: Hello\n"; }

Replies are listed 'Best First'.
Re: Substring comparisson problem
by golux (Chaplain) on Nov 27, 2013 at 19:16 UTC
    Hi ChTidio,

    It's because you're looking for the substring "Hello" with the quotes. Get rid of them and try again:

    my $string = "Hello World"; if ($string =~ /Hello/) { print "Basic String contains: Hello\n"; }

    Update:   You could also put quotes around 'Hello' with any of the following (among other possibilities):

    my $string = "\"Hello\" World"; my $string = '"Hello" World'; my $string = q{"Hello" World}; my $string = qq{"Hello" World};

    Then your original regex should find the quoted substring "Hello":

    if ($string =~ /"Hello"/) { print "Basic String contains: Hello\n"; }
    say  substr+lc crypt(qw $i3 SI$),4,5
      Thank you! That was it. After your reply, I went back and double-checked the book I am using for a reference (an older book on Perl 5) and it included the double quotes - exactly as I had typed the line. My trust in the rest of the material in that book has just diminished. Will have to look for a better/newer book. Thanks again for the prompt and complete reply!
        Not that having a newer, shinier book is not nice, but if $$$ count, do two things before diminution of trust is a absolute reason to buy a new one:
        1. Check in internet (publisher's site) for a listing of errata. Good books tend to have them. Reliable publishers tend to post them.
        2. Check the head section for an explanation of any formatting conventions. Granted, what you've quoted sounds pretty-non-standard and confusing, well-edited books on any programming language tend to have a set of conventions that are explained "up front" (for some somewhat variable value of "up front") and then used without further ado.

        And if/when you do go looking for a new book, I recommend that you search first, foremost, and likely last at O'Reilly and Apress. Not all publishers of texts on Perl can/do provide reference or tutorials that come even close, although you may want to search this site for some recommended sources -- in print and online.

        Jumping to conclusions is poor exercise!

Re: Substring comparisson problem
by pablopelos (Sexton) on Dec 02, 2013 at 00:51 UTC
    I would recommend 'Modern Perl', it can be found on the net for free in a variety of formats.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064688]
Front-paged by lidden
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2020-02-26 07:09 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (113 votes). Check out past polls.