Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: string/array smart match regex failing me

by JavaFan (Canon)
on Jan 05, 2011 at 21:01 UTC ( [id://880677]=note: print w/replies, xml ) Need Help??


in reply to string/array smart match regex failing me

Considering none of the strings in @pattern equals $string, I'm not surprised.

You cannot use ~~ to match a given string with an array of patterns. You can do the opposite: match a pattern against an array of strings.

But you're matching a string against an array. And that is true iff there's an exact match of the string with at least one element of the array.

See the perlsyn manual page.

  • Comment on Re: string/array smart match regex failing me

Replies are listed 'Best First'.
Re^2: string/array smart match regex failing me
by ikegami (Patriarch) on Jan 05, 2011 at 22:48 UTC

    You cannot use ~~ to match a given string with an array of patterns.

    Yes you can. From perlsyn of 5.10.1, 5.12.0 and 5.12.2, the first matching case is

    Any ~~ Array match against an array element grep $a ~~ $_, @$b

    which repeatedly does

    Any ~~ Regex pattern match $a =~ /$b/

    Example:

    use feature qw( say ); my @pats = map qr/$_/, qw( a c ); for (qw( aaa bbb ccc ddd )) { say if $_ ~~ @pats; }
    aaa ccc

    I didn't check the 5.10.0 docs since smart matching underwent major fixes in 5.10.1.

Re^2: string/array smart match regex failing me
by GrandFather (Saint) on Jan 05, 2011 at 23:55 UTC
    You cannot use ~~ to match a given string with an array of patterns

    except that toolic's reply demonstrates that it does work and the perlsyn docs include a table in the 'Smart matching in detail' section that includes:

    $a $b Type of Match Implied Matching Code ====== ===== ===================== ============= ... Any Array match against an array element[3] grep $a ~~ $_, @$b ...

    which clearly shows that an element by element smart match is performed, so an array of Regexs should work as the OP expects (and as toolic confirms). My best guess is that the OP is using 10.0 which introduced the smart match operator and a pile of bugs. 10.1 and later fixes a pile of smart match related bugs.

    The brian d foy post linked by the OP is rather more explicit that the technique works than the Perl docs and, given the author, is fairly likely to be reliable.

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://880677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-23 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found