Nico has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone!

I have run into a dilemma and I can't seem to find my way out.

I have a text file, and in that text file (somewhere) is a list of .txt files. These .txt files are listed in this text file because they were created in a directory due to some environmental variables.

I want to be able to take the text file names that were generated out of the text file, and compare it to a list of files in a directory and run searches against the files that come back with a positive match.

Here is the code I have for getting the list of files in a directory:

my $directory_path = undef; my @directory_path_file_list = undef; opendir DIR, $directory_path or die "Cannot open directory $directory_ +path"; @directory_path_file_list = readdir DIR; closedir DIR;
I then have the following code for getting the list of file names out of the text file:
my $text_file = undef; my @text_file_names = undef; $text_file = "text_file_with_names.txt"; open (my $fh, '<', $text_file) or die("Can't open input file \"$text_file\": $!\n"); while (my $line = <$fh>) { chomp $line; my @strings = $line =~ m/=(\s+\S+.txt)/x; foreach my $s (@strings) { @text_file_names = $1; } }

The way this works right now, is I am able to see the array of all the current files in the directory, and I can pull the name of the .txt file out of the second array one by one, but I don't know how to compare them to catch positive matches. I could be going at this the completely wrong way, which I am fully ready to accept!

Like I mentioned earlier in the post, I want to be able to compare the text file that contains the list of generated text file names and the current list of files in the directory path. Once they are compared I want to act upon the file name of the positive matches.

Please let me know if that doesn't make sense, I'm trying to explain it the best I can.

Thank you!

Replies are listed 'Best First'.
Re: Gather List of Files from Text - Compare Against List of Files from Directory
by Eily (Monsignor) on Mar 01, 2016 at 17:31 UTC

    It looks to me like you don't even need to get the list of files in the directory. Just use -e to check that a file in your list exists in the folder:

    foreach my $file (@strings) { next unless -e $file; # ignore this file if it doesn't exist # your code here }

    NB: when declaring a scalar with my, the undef is implicit, so my $scalar; is enough. But when you write an array my @array; is an empty array, but my @array = undef; is an array with one undefined element ; the size of the array is 1 instead of 0.

      Thank you!

      Each file is going to have different commands executed against it. Is that possible with this code? Or do I need to get a list of commands to run against each file it found?

Re: Gather List of Files from Text - Compare Against List of Files from Directory
by stevieb (Canon) on Mar 01, 2016 at 17:51 UTC

    Perhaps something like the following?

    First, I mocked up your input file as you didn't give an example:

    = a.txt = c.txt = b.txt = e.txt

    Here's the example code. Note I've changed your directory handle to a lexical one as opposed to a bareword global. Comments inline:

    use warnings; use strict; my $directory_path = '.'; opendir my $dir, $directory_path or die "Cannot open directory $direct +ory_path"; # create a hash with the files in the dir # (makes lookup faster later on) my %directory_path_file_list = map { $_ => 1 } readdir $dir; closedir $dir; my $text_file = "text_file_with_names.txt"; open (my $fh, '<', $text_file) or die("Can't open input file \"$text_file\": $!\n"); # define a new array for the files listed in the file, # extract the names, and push each filename onto the new # array my @files_in_file; while (my $line = <$fh>) { if ($line =~ /=\s+(\w+\.txt)/){ push @files_in_file, $1; } } # loop over the files found in the file, then check to see # if each one exists in the hash we created earlier. # you can perform your actions inside of the 'if' statement for (@files_in_file){ if (defined $directory_path_file_list{$_}){ print "$_ file exists on disk\n"; } else { print "$_ file doesn't seem to exist\n"; } }

    Update: It slipped my mind to use -e as Eily suggested, but that's the way I'd go as well.