Angus Glanville wrote:
Hi,
Hello,
I here is a sample of the problem I am trying to solve.
I have an index.txt file that contains two values separated by a pipe
symbol like this:
junk_file_test1|test1.pdf
junk_file_test2|test2.pdf
I slurp the file in, open a directory handle and try to compare the
value to the right of the pipe to the file names in the directory. My
final goal is to normalize the names so that they are the same case on
the file system as indicated in the index file. I think this will be a
simple rename. however, at this point when I run through my while loop
I only get one file name output when the script ends, but I expect to
see multiple matches. It seems like the all the contents of the file
index are run against one iteration of the directory contents then it
quits.
I have changed the list assignment from the split function to an array
and added a foreach loop there but that seems to provide the same output.
Here is a copy of my current code snippet:
#!/usr/bin/perl
#
use strict;
use warnings;
my $index = "/tmp/www/index.txt";
my $pdf_dir = "/tmp/www";
open (INDEX, "$index") || die "Can't open $index: $!\n";
opendir (PDFDIR, $pdf_dir) || die "Can't open $pdf_dir: $!\n";
while (my $line = <INDEX> ) {
chomp $line;
my ($raw_name, $std_name) = (split /\|/, $line);
if (grep {$std_name} readdir(PDFDIR)) {
readdir() produces a list of all the names in $pdf_dir and then every
subsequent use of readdir() produces undef. grep() uses a boolean test
so the value of $std_name is tested for true or false and since it is
always true all values from readdir() are passed through.
print "I found this: $std_name\n";
}
}
You probably need to use the -e (exists) file test operator:
while ( my $line = <INDEX> ) {
chomp $line;
my ( $raw_name, $std_name ) = split /\|/, $line;
if ( -e "$pdf_dir/$std_name" ) {
print "I found this: $std_name\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/