Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic

Status
Not open for further replies.

DaRedMonkey

IS-IT--Management
Aug 23, 2002
17
US
How come this doesn't work? I keep beating my head, but I just couldn't figure it out?

$filepattern =~ /^[a-z,A-Z]{3}\d{6}\.\d\d$/;
$fullpath = $datadir."/".$filepattern;
if (-e $fullpath) {
blah blah
}


In the directory I have files:

ABC123456.01
ABC123456.02
ABC123456.03
CDE123456.08

I would check to see if there is any file in that directory with that pattern, if the file exist, then I would add the name of the file to an array. Then, I would use that array ot iterate through and process each file.

Any help would be greatly appreciate. Thans alot.

-DRM
 
You're matching a variable $filepattern to conform to your pattern, where or how is this variable being assigned?

I'm not gr8 at regexes, but it looks OK.

Are you using File::Find, or opendir?

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Scratch that, I see you're using File::Glob.

Could you post the relevant portions of your code so we can see the assignments. If the assignments aren't working, there's little chance of the regex matching

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
The following works:

use Cwd;
use File::Find;
$datadir=&quot;<your directory here>&quot;;
chdir $datadir;
find sub { $fileinlist [ $i++ ] = $File::Find::name }, '.';# obtain list of all files
foreach my $filename (@fileinlist) {
print &quot;$filename\n&quot;;
if ($filename =~ /^[a-z,A-Z]{3}\d{6}\.\d\d$/) {
$fullpath = $datadir.&quot;/&quot;.$filepattern;
push(@files, $fullpath);
}
}
foreach $entry(@files) {
if (-e $entry) {
print &quot;$entry exists\n&quot;;
#do processing
}
}
 
The problem in the original post is the caret (^) character. It will match the beginning of a line and yet it is in the middle of your $fullpath variable.

For example, if $datadir is '/tmp', then $fullpath is
[tt]/tmp/^[a-z,A-Z]{3}\d{6}\.\d\d$/[/tt]
where you probably intend
[tt]^/tmp/[a-z,A-Z]{3}\d{6}\.\d\d$/[/tt]

Yours,

fish


&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 

The solution offered by TonyKent worked!

Thank you to everyone that responded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top