niceguy has asked for the wisdom of the Perl Monks concerning the following question:
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; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare 2 arrays
by GrandFather (Saint) on Jun 28, 2016 at 00:00 UTC | |
by Marshall (Canon) on Jun 28, 2016 at 00:59 UTC | |
by niceguy (Initiate) on Jun 28, 2016 at 15:58 UTC | |
by Marshall (Canon) on Jun 28, 2016 at 18:34 UTC | |
by niceguy (Initiate) on Jun 28, 2016 at 15:49 UTC | |
by GrandFather (Saint) on Jun 28, 2016 at 21:03 UTC | |
|
Re: Compare 2 arrays
by Anonymous Monk on Jun 28, 2016 at 00:31 UTC | |
|
Re: Compare 2 arrays
by stevieb (Canon) on Jun 28, 2016 at 16:41 UTC | |
by niceguy (Initiate) on Jun 28, 2016 at 17:19 UTC | |
by Marshall (Canon) on Jun 29, 2016 at 12:27 UTC | |
by niceguy (Initiate) on Jun 29, 2016 at 22:10 UTC | |
by Marshall (Canon) on Jun 30, 2016 at 00:01 UTC | |
|