Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Regex vs. string

by BaldPenguin (Friar)
on Jul 08, 2005 at 23:01 UTC ( #473603=perlquestion: print w/ replies, xml ) Need Help??
BaldPenguin has asked for the wisdom of the Perl Monks concerning the following question:

I ran across this is code, and while my first guess was that it shouldn't work, it works well. I am just trying to wrap my head around why is does.
use strict; use warnings; my $current_index = 12; my %columns = ( 'alpha' => 1, 'bravo' => 2, 'charlie' => 3, 'delta' => 4, ); foreach my $k ( keys %columns ) { ( $current_index =~ $columns{$k} ) ? print "$k matches\n" : print "$k does not match\n"; }
bravo matches charlie does not match delta does not match alpha matches
I am trying to grasp why this works, why word perl automagically interpret the scalar as a regex?


Comment on Regex vs. string
Select or Download Code
Re: Regex vs. string
by Enlil (Parson) on Jul 08, 2005 at 23:05 UTC
    From perlop:

    Binary ``=~'' binds a scalar expression to a pattern match. Certain operations search or modify the string $_ by default. This operator makes that kind of operation work on some other string. The right argument is a search pattern, substitution, or transliteration. The left argument is what is supposed to be searched, substituted, or transliterated instead of the default $_. When used in scalar context, the return value generally indicates the success of the operation. Behavior in list context depends on the particular operator. See Regexp Quote-Like Operators for details.

    If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time. This can be less efficient than an explicit search, because the pattern must be compiled every time the expression is evaluated.

    Binary ``!~'' is just like ``=~'' except the return value is negated in the logical sense.


Re: Regex vs. string
by dave_the_m (Parson) on Jul 09, 2005 at 00:34 UTC
    Note also that there are some subtle differences between a literal pattern, and a string used as a pattern, eg
    $foo =~ /\ba\b/; # match the word "a" $foo =~ "\ba\b"; # match the three characters # backspace(\x08), "a", backspace

    update: changed bell to backspace as per TimToady's nitpick


      Mmm, actually, \x08 is a backspace, to pick small nit. A \a would produce a bell (though it's not clear whether we'll carry that one over to Perl 6).
Re: Regex vs. string
by kwaping (Priest) on Jul 09, 2005 at 15:03 UTC
    In simpler terms, changing your =~ to == will probably give you the results you were expecting.

    What's happening is that you're currently asking whether the character on the right is anywhere inside the string on the left. That's why alpha ("1") and bravo ("2") are successful when the string is "12".
Re: Regex vs. string
by kp7 (Initiate) on Jul 10, 2005 at 16:28 UTC
    from perlop manpage, about =~ operator :

    " If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time."

    -- regards piotr

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-07-14 10:28 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (257 votes), past polls