It looks like (?: .. ) is something special and is not returning a numbered variable so all we get out are two variables $1 and $2 used respectively in the hash as the key and value?
You bet. ;-) The
?: in the bracket tells Perl not to capture the pattern inside the bracket. You can find the documentation on
(?:pattern) on the CPAN perlre documentation
here
And could I check for a string contain anything from a list?
Well, yes you can. The method I use is to construct the search pattern with a join, as the following example demonstrates -
my $something = 'This is a bang of a bing thing';
my @list = qw /bing bong bang/; # want to search for these
my $list = join '|', @list; # construct my pattern
if($something =~ m/($list)/i) {
print "Found '$1' in '$something'\n";
}
If you want to capture all occurances of the patterns, you could use the
@array = $str =~ m/pattern/g idiom.
my @search = $something =~ m/($list)/ig; # <- added the g modifier
or you could do this in a while loop -
while ($something =~ m/($list)/ig) {
print "Found '$1' in '$something'\n";
}
The problem with your code is that
m/(@list)/i is looking for the pattern of the interpolated list items, the pattern "bing bong bang", in the string, and of cause it is not found.
use strict;
my $something = 'This is a bang of a bing thing bing bong bang';
my @list = qw / bing bong bang /;
if ($something =~ m/(@list)/i) {
print "Found '$1' in '$something'\n";
}
And the output is -
Found 'bing bong bang' in 'This is a bang of a bing thing bing bong ba
+ng'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.