Wolf Blaum wrote:

#!/usr/bin/perl
use strict;
use warnings;

my $file;
my @xfiles;

@xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine",
".hidden");

foreach $file (@xfiles){

#want canada only for this iteration
        if ($file =~ /(canada)/){print "$file\n  - end first if -  \n";}

#wb: (expression) groups what you want to find - ie dont search for c,a,n,d...
but for the whole word "canada" ,

Parentheses *can* be used for grouping but they are not really needed here. They can also be used for capturing but they are not needed for that either.


no next required since if you match
canada, elsif wil not be executed (thats what else means, right?)

Correct.


#then want cane, canine, ca.e.02
elsif ($file =~ /(^cane$)|(canine)|(ca.e.02)/){ print "$file\n"; }
  }

#wb: same as above, | lists alternatives, ie either "cane" or "canine" or ..
^ matches beginning of word, $ matches end

Again, the parentheses are superfluous as they are not needed for capturing or grouping. The ^ (beginning of line) and $ (end of line) anchors only apply to the pattern 'cane', the other alternations can match anywhere in the string. The pattern /ca.e.02/ will match 'ca.e.02' but it will also match 'camel02' because the . meta-character will match any character except newline. If you want to match a literal . character then you have to escape it: /ca\.e\.02/.

It looks like you intended to use the pattern:

/^(?:cane|canine|ca\.e\.02)$/

or better:

/^ca(?:ne|nine|\.e\.02)$/



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to