Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Creating an array from a text file

by mikevanhoff (Acolyte)
on Aug 13, 2002 at 16:33 UTC ( [id://189863]=note: print w/replies, xml ) Need Help??


in reply to Re: Creating an array from a text file
in thread Creating an array from a text file

aufrank and all responders; Thank you for the explaination and suggestions on asking questions. All of these responses will be of some help, even the oneliner. Although I understand the concepts of opening and reading from file, understanding how the array is created, and actually substuting the elements into my code is vague to me. I have included my code so that my question is more explicit. I am trying to delete the files in the reports directory that are x days old. I have 67 directories under /opt/web that all have reports directories. I would like to place all of these paths into a text file to be read into the array, and have the following actions performed.
# open the directory for reading chdir "/opt/web/hr83tst/reports"; opendir(REP, "/opt/web/hr83tst/reports") || die "Cannot open the dire +ctory /opt/web/hr83/reports $! "; open(remlog,">/Scripts/dir-removed.log"); # open a file to keep as a +log of directories removed. print remlog "Directories removed this date : ",`date`, "\n"; # list the contents of the directory. These should be directories. while ($name = readdir(REP)) { if (-M $name >= 10) { print "$name\n" unless($name eq "images"); print remlog "$name\n" unless($name eq "images"); `rm -r $name` unless($name eq "images"); # THIS WILL REMOVE THE D +IRECTORY AND ALL OF ITS subdirectories and Files. } } close remlog; closedir(REP)

Replies are listed 'Best First'.
Re: Re: Re: Creating an array from a text file
by insensate (Hermit) on Aug 14, 2002 at 15:24 UTC
    You can also use the File::Find module as follows to populate your array:
    use File::Find; find sub { push @reportdirs, $File::Find::name, if -d && /^reports$/ } +, "/opt/web";
    That way, you won't have to modify your file every time your directory structure changes. You then can readdir each directory in the array to begin processing the files it contains.
    Jason

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://189863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found