Home > Archive > PERL Beginners > March 2005 > Interpolation Problem with Matching
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Interpolation Problem with Matching
|
|
| Jim Richards 2005-03-09, 3:56 am |
| I am trying to compare files names listed in a file to the actual files
in a directory. My code is as follows:
Opendir(DIR,"name");
@files=readdir(DIR);
Open(IN,"<fileList");
While(<IN> ) {
If(/^pattern/) {
moveFile($_);
}
}
Close(IN);
Sub moveFile() {
My $src=@_[0];
Foreach(@files) {
If(/$src/) {
Print "$_\n";
}
}
}
I know this is not the most efficient, but I do not understand why the
statement if(/$src/) is never true? Does not seem the $src is
interpolated correctly for the match.
Why is this happening? How can it be fixed?
Thanks in advance!!!
Jim
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2005-03-09, 3:56 am |
| RICHARDS, JIM wrote:
> I am trying to compare files names listed in a file to the actual
> files in a directory. My code is as follows:
>=20
>=20
>=20
> Opendir(DIR,"name");
>=20
> @files=3Dreaddir(DIR);
>=20
>=20
>=20
> Open(IN,"<fileList");
>=20
>=20
>=20
> While(<IN> ) {
>=20
> If(/^pattern/) {
>=20
> moveFile($_);
>=20
> }
>=20
> }
>=20
>=20
>=20
> Close(IN);
>=20
>=20
>=20
> Sub moveFile() {
>=20
> My $src=3D@_[0];
>=20
> Foreach(@files) {
>=20
> If(/$src/) {
Since you are using $src as the pattern, it will take any slashes it finds=
and take something like abc\g and look for abcg since a \ in pattern takes=
whatever the next character is. So even thouhg you see abc\g, the pattern=
is looking for abcg. Wrap the pattern in \Q$src\E which turns off any of =
the special characters.
Wags ;)
>=20
> Print "$_\n";
>=20
> }
>=20
> }
>=20
> }
>=20
>=20
>=20
>=20
>=20
> I know this is not the most efficient, but I do not understand why the
> statement if(/$src/) is never true? Does not seem the $src is
> interpolated correctly for the match.
>=20
> Why is this happening? How can it be fixed?
>=20
>=20
>=20
> Thanks in advance!!!
>=20
>=20
>=20
> Jim
****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************
| |
| thundergnat 2005-03-09, 3:56 am |
| Jim Richards wrote:
> I am trying to compare files names listed in a file to the actual files
> in a directory. My code is as follows:
use strict;
use warnings;
>
> Opendir(DIR,"name");
Perl is case sensitive. Always (ALWAYS!) check to se if an open
sucededed. Use lexical File handle names. Avoid double quotes where
single quotes are mre appropriate
opendir(my $dir, 'name') or die "Could not open directory. $!";
>
> @files=readdir($dir);
Use scoped varibles. It is not AS critical for a script this short,
but it is a good habit to get into (and necessary when using strict.)
Be aware that readdir() DOES NOT return the directory name. If you
want to operate on a file that readdir returns, you'll need to either
chdir to the directory or prepend the directory name.
my @files = readdir($dir);
>
> Open(IN,"<fileList");
Perl is case sensitive. Always (ALWAYS!) check to see if an open
sucededed. Should use lexical file handles and three argument opens.
open my $in, '<', 'fileList' or die "Could not open file. $!";
>
> While(<IN> ){
Perl is case sensitive.
while (<$in> ){
>
> If(/^pattern/) {
Perl is case sensitive.
if (/^pattern/){
>
> moveFile($_);
I sincerely doubt that any of the actual files have a newline in
the filename. You need to chomp() the filename.
chomp;
moveFile($_);
>
> }
>
> }
>
>
>
> Close(IN);
Perl is case sensitive. Not necessary with lexical file handles.
>
>
>
> Sub moveFile() {
Perl is case sensitive. Dont use prototypes unless you really need them.
sub moveFile{
>
> My $src=@_[0];
Perl is case sensitive. You don't really want an array slice. Just shift
the value off the @_ array.
my $src = shift;
>
> Foreach(@files) {
Perl is case sensitive. I don't really understand what you are trying
to accomplish here. You seem to be going through the array trying to
find if the filename you just got out of it, is in it. This should
NEVER fail. Just skip the test.
>
> If(/$src/) {
Perl is case sensitive. Unnecessary, see above.
>
> Print "$_\n";
Perl is case sensitive.
print "$src\n";
>
> }
>
> }
>
> }
>
> I know this is not the most efficient, but I do not understand why the
> statement if(/$src/) is never true? Does not seem the $src is
> interpolated correctly for the match.
>
> Why is this happening?
Because you have too many syntax errors. The code is never executed.
If you had used warnings, you would have known that.
> How can it be fixed?
#UNTESTED#
use strict;
use warnings;
my $directory = 'name';
my $filelist = 'fileList';
opendir(my $dir, $directory) or die "Could not open directory. $!";
my @files = readdir($dir);
open my $in, '<', $filelist or die "Could not open file. $!";
while (<$in> ){
if (/^pattern/){
chomp;
moveFile($_);
}
}
sub moveFile{
my $src = shift;
print "$src\n";
}
| |
| John W. Krahn 2005-03-09, 3:56 am |
| RICHARDS, JIM wrote:
> I am trying to compare files names listed in a file to the actual files
> in a directory. My code is as follows:
Your "code" won't compile. Please add the following two lines:
use warnings;
use strict;
to the top of your program and let perl help you find the mistakes.
The best bet to solve your problem is to use a hash for the file names,
something like:
my $filelist = '/home/jim/filelist';
open FILES, '<', $filelist or die "Cannot open $filelist: $!";
my %files;
while ( <FILES> ) {
chomp;
$files{ $_ }++;
}
close FILES;
my $dir = '/home/jim/somedir';
opendir DIR, $dir or die "Cannot open $dir: $!";
while ( my $file = readdir DIR ) {
if ( exists $files{ $file } ) {
print "$file\n";
}
}
closedir DIR;
John
--
use Perl;
program
fulfillment
| |
| thundergnat 2005-03-09, 3:56 am |
| thundergnat wrote:
>
>
> Perl is case sensitive. I don't really understand what you are trying to
> accomplish here. You seem to be going through the array trying to find
> if the filename you just got out of it, is in it. This should
> NEVER fail. Just skip the test.
Doh! I just went back and reread the post and my moron moment cleared up.
#STILL UNTESTED#
use strict;
use warnings;
my $directory = 'name';
my $filelist = 'fileList';
opendir(my $dir, $directory) or die "Could not open directory. $!";
my @files = readdir($dir);
open my $in, '<', $filelist or die "Could not open file. $!";
while (<$in> ){
if (/^pattern/){
chomp;
moveFile($_);
}
}
sub moveFile{
my $src = shift;
print "$_\n" if /$src/ for @files;
}
|
|
|
|
|