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

i am having a folder Dir_1.
it has many files like
file1
DOC ( this is a Directory)
file2
after i execute my test the folder contents change like
file1
file2
<file3
DOC
DIR_2
i had written a procedure which does folowing thing
it accepts a array as arguments which has all files names in current working Directory. all files before execution of script are in this array
inside the procedure again i define a array which contain all the files in current working Directory. this will include all files generated by test script.now i want to delete those files which were in old array and not in new array.
one way of doing is to compare each file of both arrays using for loop but this is highly ineffcient. can you tell me to do same in much efficient manner
  • Comment on memorizing files and then removing the unnecessary ones

Replies are listed 'Best First'.
Re: memorizing files and then removing the unnecessary ones
by BrowserUk (Patriarch) on Feb 28, 2005 at 11:51 UTC

    What you want is the difference between the two arrays. That can be computed like this.

    #! perl -slw use strict; my @before_files = map{ chr 65+rand 26 } 1.. 4; my @after_files = ( @before_files, map{ chr 65+rand 26 } 1.. 6 ); my %seen; @seen{ @before_files } = (); my @only_new_files = grep{ not exists $seen{ $_ } } @after_files; print "@before_files\n@after_files\n@only_new_files"; __END__ P:\test>junk2 Y B Y A Y B Y A P E F N L U P E F N L U

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: memorizing files and then removing the unnecessary ones
by periapt (Hermit) on Feb 28, 2005 at 13:58 UTC
    BrowserUk's solution might be a little more clear (if a little less efficient) if it is viewed this way. Use one hash and one array. Store the initial set of file names as keys in a hash and store the second set in an array. You then only need to compare the list of files in the second set against the hash. Every file that doesn't exist in the hash can be deleted.
    my %set01 = (); $set01{$_} = 1 foreach (glob("*.*"); # identify each file in director +y ..... # code that creates secondary files # my @new_files = (); # keep a list of +new files if you want my @set02 = glob("*.*"); foreach (@set02){ # push @new_files, $_ unless(exists($set01{$_})) unlink $_ unless(exists($set01{$_})); # delete files that weren't + aren't in original set } print "original files\n",sort keys %set01,"\nnew files\n",sort @set02, +"\n";
    You can shorten/improve this code any number of ways but it does rather simply show what needs to be done

    PJ
    use strict; use warnings; use diagnostics;
Re: memorizing files and then removing the unnecessary ones
by dorward (Curate) on Feb 28, 2005 at 11:13 UTC
      You should use [id://...] for links to other nodes. If you want to change the text of the link, then you can do [id://...|New link text]. This will allow your node to work in the face of changes to the perlmonks backend, and also will not take people to a different domain (e.g. I browse from http://www.perlmonks.org, but your link is to http://perlmonks.org, thus my cookie isn't being read when I click on your link, and I end up logged in as Anonymonk).
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: memorizing files and then removing the unnecessary ones
by manav (Scribe) on Mar 01, 2005 at 12:46 UTC
    Do you want to do something like
    my %myhash=map{$_ => 1}@old_arr ; @new_arr=grep{!$myhash{$_}}@new_arr;

    Manav