Dear PerlMonks,
I am relatively new with Perl. I am trying to delete some old files from a directory based on, if it is not listed in the file and this file has a SDF file extension.
I have an array (@file) that contains a list of all the file name in the directory. And another array (@name) contains a list of all the lines from the SDF file.
What I like to do is to compare the two arrays and find the file name that is not listed in the SDF file.
The SDF file contains lines like:
I've been searching the web and found some good examples but not exactly on what I am trying to do. I have tried using "!($name =~ $filename)", it just delete all the files in the directory. I think there is something wrong with the logic in the loop but not sure where. Below, is my code that are able to find the matching files. I am wondering if you can help.
Thank you in advance for your help.
#!\perl\bin\perl use strict; use warnings; my $files = "C:\\Directory"; my $list = "C:\\Test.sdf"; my @name; open(my $name, "< $list") or die "Failed to open file: $!\n"; while(<$name>) { chomp; push @name, $_; } close $name; my @file = $files; opendir(OUTPUT, $files); @file = readdir(OUTPUT); closedir(OUTPUT); foreach my $filename (@file) { foreach $name (@name) { if ($name =~ $filename) { last; }else { unlink ($files . "\\" . $filename) or warn qq{cannot delete $fil +ename: $!+}; last; } } }
In reply to Compare 2 arrays by niceguy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |