odd regexp behaviour (?)

Discussion in 'Perl Misc' started by bengt wikenfalk, Oct 10, 2003.

  1. Hi.

    I wonder about the difference between =~ and = ~

    I wrote a script with the following expression:


    return $c if ($name = ~ /^OK$/);
    ($name is an array reference (ARRAY=0x....)
    This works ! (if the array contains the string "OK", $c will be returned.

    if I change the code to $name =~ ... it does not work however (which
    actually makes me relieved ..)


    Is this a standard feature ? Can anyone describe what happens here ?

    (I'm running Perl 5.6 under cygwin)
     
    bengt wikenfalk, Oct 10, 2003
    #1
    1. Advertising

  2. bengt wikenfalk <> wrote:
    > I wonder about the difference between =~ and = ~


    "=~" is one operator. "= ~" is two.

    #!/usr/bin/perl -l

    $_ = "foo";
    $str = "bar";

    if ($str =~ /bar/) {
    print "$str matched /bar/";
    }

    if ($str = ~ /foo/) {
    print "\$str is $str";
    print "That's ~1: ", ~1;
    }

    --
    Steve
     
    Steve Grazzini, Oct 10, 2003
    #2
    1. Advertising

  3. Steve Grazzini <> wrote:
    > bengt wikenfalk <> wrote:
    > > I wonder about the difference between =~ and = ~

    >
    > "=~" is one operator. "= ~" is two.
    >
    > #!/usr/bin/perl -l
    >
    > $_ = "foo";
    > $str = "bar";
    >
    > if ($str =~ /bar/) {
    > print "$str matched /bar/";
    > }
    >
    > if ($str = ~ /foo/) {
    > print "\$str is $str";
    > print "That's ~1: ", ~1;
    > }


    Also:
    $_ = '';
    if ($str = ~ /foo/) {
    print "\$str is $str";
    print "That's ~0: ", ~0;
    }

    ($str = ~ /foo/) means "assign to $str the bitwise negation of the
    scalar result of matching /foo/ against $_". Note that both ~0 and ~1
    are non-zero, hence true.

    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Oct 10, 2003
    #3
    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. David Veerasingam

    Odd behaviour of regexp module

    David Veerasingam, Jul 13, 2005, in forum: Python
    Replies:
    3
    Views:
    267
  2. David Veerasingam

    Some more odd behaviour from the Regexp library

    David Veerasingam, Oct 20, 2005, in forum: Python
    Replies:
    4
    Views:
    291
    David Veerasingam
    Oct 20, 2005
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,159
    Richard Heathfield
    Feb 18, 2007
  4. Kyle Heck

    Odd Regexp Issue

    Kyle Heck, May 23, 2007, in forum: Ruby
    Replies:
    7
    Views:
    130
    Jenda Krynicky
    May 25, 2007
  5. Joao Silva
    Replies:
    16
    Views:
    408
    7stud --
    Aug 21, 2009
Loading...

Share This Page