John Lin wrote:
> 
> Hi List,

Hello,

> This is probably a really simple question, but I need some help with this.
> Basically,  I am searching through a directory and if something matches
> what I am looking for, then save that filename to a text file.  Something
> like this:
> ===================================
> $wanted_filename='thisfilename';
> 
> opendir (DIRECTORY, "/directory/");
>         @filenames=readdir(DIRECTORY);
> closedir(DIRECTORY);
> 
> for ($i=0; $i <= $#filenames; $i++) {
>         [EMAIL PROTECTED];
>         if ($wanted_filename == $filename) {
>                 $append_filename=$filename . ';';
>                 open(APPENDFILE, ">>/anotherdirectory/filename.txt");
>                         print APPENDFILE $append_filename;
>                 close(APPENDFILE);
>         }
> }
> ====================================
> 
> But for some reason, the IF statement did not work.  In the filename.txt
> file, I am getting something like this:
> 
> .;..;file1;file2;thisfilename;file3;.....
> 
> Basically the IF statement did not filter out the files that I didn't
> want.  The only filename that I wanted to store, if it's found in the
> directory, is "thisfilename".  Any help is appreciated.  Thanks.

You are using the numerical comparison operator '==' but you are comparing
strings so you need to use 'eq' instead.  Also your for loop is very C-ish,
a more perl-ish loop would look like:

my $wanted_filename = 'thisfilename';

opendir DIRECTORY, '/directory' or dir "Cannot open /directory: $!";
my @filenames = readdir DIRECTORY;
closedir DIRECTORY;

for my $filename ( @filenames ) {
    if ( $wanted_filename eq $filename ) {
        $append_filename = $filename . ';';
        open APPENDFILE, '>>/anotherdirectory/filename.txt'
            or die "Cannot open /anotherdirectory/filename.txt: $!";
        print APPENDFILE $append_filename;
        close APPENDFILE;
    }
}


If the file that you are appending to is the same for every entry in @filenames
then there is no point in opening and closing it in the loop.

my $wanted_filename = 'thisfilename';

opendir DIRECTORY, '/directory' or dir "Cannot open /directory: $!";
my @filenames = readdir DIRECTORY;
closedir DIRECTORY;

open APPENDFILE, '>>/anotherdirectory/filename.txt'
    or die "Cannot open /anotherdirectory/filename.txt: $!";

for my $filename ( @filenames ) {
    if ( $wanted_filename eq $filename ) {
        print APPENDFILE $filename, ';';
    }
}
close APPENDFILE;


Of course, you could also do it without the for loop.  :-)

my $wanted_filename = 'thisfilename';

opendir DIRECTORY, '/directory'
    or dir "Cannot open /directory: $!";
open APPENDFILE, '>>/anotherdirectory/filename.txt'
    or die "Cannot open /anotherdirectory/filename.txt: $!";

print APPENDFILE map "$_;", grep $wanted_filename eq $_, readdir DIRECTORY;

closedir DIRECTORY;
close APPENDFILE;




John
-- 
use Perl;
program
fulfillment

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


Reply via email to