PERL-How to calculate the largest directory excluded subdirectories?

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Hi,

I just start up with PERL.And I got a question about if I've been given a zip file which has a lot of files and directories in it.I need to output the largest directory without including the subdirectories.How to sum all the files in the directory without summing the size of the subdirectory?
Look forward to hearing from you.

I need to make use of hash and regular expression.
 

bwack

Joined Nov 15, 2011
113
http://perldoc.perl.org/ is your friend.

Your task sounds good for learning hashes. Shall the PERL program read the directory structure on your harddrive or shall it take information from a zip file ? I assume the latter because you're gonna use regular expressions. ..? and I also assume you're not going to use any PERL modules like IO::Uncompress::Unzip ?
What OS are you using?

What you could do is to pipe the output of the unzip program into your perl script. You launch your program like this : "unzip zipfilepath --list |perl youperlprogram.pl" (where the "|" is the pipe). Reading from a pipeline is so easy --> http://www.perlmonks.org/?node_id=76108
 
Last edited:

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
http://perldoc.perl.org/ is your friend.

Your task sounds good for learning hashes. Shall the PERL program read the directory structure on your harddrive or shall it take information from a zip file ? I assume the latter because you're gonna use regular expressions. ..? and I also assume you're not going to use any PERL modules like IO::Uncompress::Unzip ?
What OS are you using?

What you could do is to pipe the output of the unzip program into your perl script. You launch your program like this : "unzip zipfilepath --list |perl youperlprogram.pl" (where the "|" is the pipe). Reading from a pipeline is so easy --> http://www.perlmonks.org/?node_id=76108

The information about size,username,and path will be input in a zip file.Then I will have to get information from there.Sum all the files in the directory without including the subdirectories in that directory which is my zip file.
I am using Eclipse in WIndow and also using VNC to run UNIX.Now my 1st step is to split the[size][username][path].But then I dont know how to cont.How to put those information into hash and how to calculate to get the largest directory.Could you tell me in details?
 

bwack

Joined Nov 15, 2011
113
What you want to do is to skip any path that is deeper than 1 levels deep, I think. You can do that by splitting the path string by "/" into a list, and count the number of elements in that list. On that list, use the scalar function to get the number of elements.
http://perldoc.perl.org/functions/split.html

You use $directory as key for a hash %sums and add the size to that sum.
Pitfall: at first the sums aren't initialized to 0. You first check if exists($sums{$directory}), and if not then you _copy_ the size to the hash, if the key exists then you _add_ the size because it is then allready initialized..
http://perldoc.perl.org/functions/defined.html
http://perldoc.perl.org/functions/exists.html

Finally use the keys function on the hash on a foreach loop to get all the directories that you have summed up. The foreach loop takes a list as parameter, the keys function gives you a list of keys in the hash.
http://perldoc.perl.org/functions/keys.html
http://perldoc.perl.org/perlsyn.html#Compound-Statements
Hmm when thinking about it, you could probably use the sort function too get the largest directory.. I think you should experiment with it.
http://perldoc.perl.org/functions/sort.html

See the FAQs for more interesting stuff
http://perldoc.perl.org/perlfaq4.html#How-do-I-process-an-entire-hash? (How do I process an entire hash)

For complete understanding of how to use hashes you would also need to learn references.
http://perldoc.perl.org/perlreftut.html
 
Last edited:

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
What you want to do is to skip any path that is deeper than 1 levels deep, I think. You can do that by splitting the path string by "/" into a list, and count the number of elements in that list. On that list, use the scalar function to get the number of elements.
http://perldoc.perl.org/functions/split.html

You use $directory as key for a hash %sums and add the size to that sum.
Pitfall: at first the sums aren't initialized to 0. You first check if exists($sums{$directory}), and if not then you _copy_ the size to the hash, if the key exists then you _add_ the size because it is then allready initialized..
http://perldoc.perl.org/functions/defined.html
http://perldoc.perl.org/functions/exists.html

Finally use the keys function on the hash on a foreach loop to get all the directories that you have summed up. The foreach loop takes a list as parameter, the keys function gives you a list of keys in the hash.
http://perldoc.perl.org/functions/keys.html
http://perldoc.perl.org/perlsyn.html#Compound-Statements
Hmm when thinking about it, you could probably use the sort function too get the largest directory.. I think you should experiment with it.
http://perldoc.perl.org/functions/sort.html

See the FAQs for more interesting stuff
http://perldoc.perl.org/perlfaq4.html#How-do-I-process-an-entire-hash? (How do I process an entire hash)

For complete understanding of how to use hashes you would also need to learn references.
http://perldoc.perl.org/perlreftut.html
Hi bwack,
Reallt thank for your information what you said is actually what I want and what i can do with my level in PERL as I just started almost for a month.and finally the result come out.Thank you!
 
Top