icarus wrote:
Hi,
Hello,
I want to display 'canada', 'cane', 'canine, 'ca.e.02'.
Problem: It only displays 'canada'
#!/usr/bin/perl
use strict;
use warnings;
my $file;
my @xfiles;
@xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine",
".hidden");
my @xfiles = ("canada", "cane", "cane02", "ca.e.02", "canine", ".hidden");
foreach $file (@xfiles){
foreach my $file ( @xfiles ) {
#want canada only for this iteration
if ($file =~ m/^ca/s){
The /s option affects whether . matches a newline or not. You are not
using . in the pattern so you don't need /s:
if ( $file =~ /^ca/ ) {
next if $file =~ m/e(\d\d)$/s; #don't want cane02
You don't use the results of capturing so you don't need the parentheses:
next if $file =~ /e\d\d$/; #don't want cane02
next if $file =~ m/e.(\d\d)$/s; #don't want ca.e.02
The . meta-character will match any character but it looks like you only
want to match a period character:
next if $file =~ /e\.\d\d$/; #don't want ca.e.02
next if $file =~ m/e$/s; #don't want cane, canine
next if $file =~ m/^\.{1}/; #skips .hidden files
The if block you are in only contains $file that match /^ca/ so it can't
match both /^ca/ and /^\./ at the same time.
print "$file\n";
}
#then want cane, canine, ca.e.02
elsif ($file =~ m/e$/s or $file =~ m/^ca\.+e$/s or $file =~ m/e.(\d\d)
$/s){
/^ca\.+e$/ will match the string 'ca.e' or 'ca..e' or 'ca...e' or
'ca.......e', etc. but it doesn't look like you have a string which
matches that pattern. Perhaps you meant /^ca.+e$/ which will match
'cane' or 'canine'. It looks like you don't need parentheses or the /s
option in any of those:
elsif ( $file =~ /e$/ or $file =~ /^ca.+e$/ or $file =~ /e\.\d\d$/ ) {
This is an elsif block which means that anything in $file at this point
will *not* match /^ca/ because the previous if block already matched
those elements. At this point the only element of @xfiles that gets
here is '.hidden'.
next if $file =~ m/^\.{1}/; #skips .hidden files
print "$file\n";
}
}
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/