Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

same program different output

by jesuashok (Curate)
on Dec 09, 2006 at 10:33 UTC ( #588776=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:


Program : I #!/usr/bin/perl use strict; my $a = "[abc][hij]"; my $b = " aj"; if ($b =~ /$a/) { print "yes"; } else { print "no"; } ________________________________________ Program : II my $a = "[abc][hij]"; my $b = " aj"; if ($a =~ /$b/) { print "yes"; } else { print "no"; }
The first program prints 'yes' the second program prints 'no'. Two program differs in there condition checking. rest all same. could anyone explain me better.

Replies are listed 'Best First'.
Re: same program different output
by Corion (Pope) on Dec 09, 2006 at 10:42 UTC

    You've just found out that =~ is not a symmetrical relation, while == is. Other asymmetrical relations are <, >, <=, >=. Other symmetrical relations are != and eq. If you want to learn more on (symmetric) relations, see Symmetric relation.

    Would it surprise you if a program that used $a < $b had output different from an otherwise identical program that used $b < $a?

Re: same program different output
by MaxKlokan (Monk) on Dec 09, 2006 at 12:19 UTC

    In program I you are checking if the regular expression /[abc][hij]/ matches the string " aj", which it does.

    In program II you are checking if regular expression / aj/ matches the string "[abc][hij]", which it does not.

Re: same program different output
by liverpole (Monsignor) on Dec 09, 2006 at 23:05 UTC

    I believe one of the reasons you are getting downvoted for this post is that it sounds a lot like a number of your previous posts, wherein you ask what sounds to be either a complete beginner's Perl question, or a question that doesn't make any sense, and then say "why is this happening?!"

    My question to you is this:  by all accounts, you've been working as a Perl programmer for at least a couple of years.  Is this really the first time you've come across regular expressions, or tried to understand how they work?  And you were unable to search for any meaningful answers, and didn't have even a single colleague who could explain it to you?  (Your junior, perhaps?)

    And if you already understand regular expressions, why are you even bothering to ask the question in the first place??

    Update:  Doing a Super Search for text containing =~, and jesuashok as author, finds (among numerous others) this node, and this one.

    You clearly understand how to use regular expressions.  Please stop wasting others' time.

Re: same program different output
by Jasper (Chaplain) on Dec 13, 2006 at 17:20 UTC
    What the heck?

    You're a fricking hermit here, you've posted 1700 times, and this is the best you can do?

    Is this some sort of exercise in patience testing, because mine ran out with you some time ago. Anytime I see one of your posts, it's all I can do to stop myself immediately downvoting it.

    If you can't think of anything to say, STOP POSTING. Please. I'm sure everyone would be helpful they felt a question was interesting or funny, or insightful. But this is just so much garbage.
Re: same program different output
by Anonymous Monk on Dec 09, 2006 at 11:52 UTC
    could anyone explain me better.
    You are uniuque :p

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://588776]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2018-10-22 11:40 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (119 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!