Perl help: Creating a multidimensional array of subdirectories and its contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl help: Creating a multidimensional array of subdirectories and its contents
# 1  
Old 02-22-2010
Perl help: Creating a multidimensional array of subdirectories and its contents

I'm currently working with dozens of FASTA files, and I'm tired of having to manually change the filename in my Perl script.

I'm trying to write a simple Perl script that'll create a 2-dimensional array containing the name of the folders and its contents.

For example, I would like the output to look something like this:

Folder A iam.fna a.fna new.fna
Folder B to.fna perl.fna
Folder C filehandling.fna
Folder D please.fna help.fna me.fna


I would like to access these folders recursively, but I am new to perl filehandling, and I'm completely clueless where to begin. Any help will be appreciated! Thank you in advance.
# 2  
Old 02-22-2010
Is this your entire directory structure or you need to handle a variable number of sub directories?
# 3  
Old 02-22-2010
Quote:
Originally Posted by radoulov
Is this your entire directory structure or you need to handle a variable number of sub directories?
That'll be my entire directory. I only have fasta files in the various folders. I would like to list out the names of folders with its contents because the names of the folders are quite similar, and I'd like to list them all out.
# 4  
Old 02-23-2010
MySQL Few ways

In perl there are lot of module to handle the files.like File::stat.also there is a simple way to read a directory.We can use the option like -d,-f,-l.Using this we can check whether the given thing is a file or directory or link file.

For example I am reading my current directory which having some number of directories .Here is the simple way,

Code:
#!/usr/bin/perl
use strict;
use warnings;


my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
         opendir(newDIR, $file) or die $!;
         print "Folder " . $file . ":\n" ;
        while(defined ($file = readdir(newDIR)))
        {
            print "$file,"; # Here you can do some specific operation on the file
        }
        print "\n\n";
        closedir(newDIR);
    }

}
closedir(DIR);

To read the get the files in current directory we can use the glob function.

Code:
my @arr=glob ".* *";

foreach(@arr)
{
    print "$_ \n";
}


Thanks
# 5  
Old 02-24-2010
Quote:
Originally Posted by karthigayan
In perl there are lot of module to handle the files.like File::stat.also there is a simple way to read a directory.We can use the option like -d,-f,-l.Using this we can check whether the given thing is a file or directory or link file.

For example I am reading my current directory which having some number of directories .Here is the simple way,

Code:
#!/usr/bin/perl
use strict;
use warnings;


my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
         opendir(newDIR, $file) or die $!;
         print "Folder " . $file . ":\n" ;
        while(defined ($file = readdir(newDIR)))
        {
            print "$file,"; # Here you can do some specific operation on the file
        }
        print "\n\n";
        closedir(newDIR);
    }

}
closedir(DIR);

To read the get the files in current directory we can use the glob function.

Code:
my @arr=glob ".* *";

foreach(@arr)
{
    print "$_ \n";
}

Thanks
Wow, thanks! It's almost perfect! I just do not want to display Folder ".."; any way around it?

---------- Post updated at 08:55 PM ---------- Previous update was at 04:55 PM ----------

Code:
#!/usr/bin/perl

open (TXT, ">directory.txt");
print TXT "Organism\t\t\tFiles\n\n";

#In current directory, list all folders and contents

my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
        opendir(newDIR, $file) or die $!;

        print TXT "\n\nFolder" . $file . ": " ;
        while(defined ($file = readdir(newDIR)))
        {
            my @arr = glob".*fna";
            foreach(@arr)
            {
                print TXT "\t$_";
            }


        }

        closedir(newDIR);
    }

I've got this much. However, I haven't been able to figure out how to print out the contents of each folder to the txt file.

---------- Post updated at 09:01 PM ---------- Previous update was at 08:55 PM ----------

Code:
#!/usr/bin/perl

open (TXT, ">directory.txt");
print TXT "Organism\t\t\tFiles\n\n";

#In current directory, list all folders and contents

my $dir = ".";
opendir(DIR, $dir) or die $!;
while(defined (my $file = readdir(DIR)))
{
    if(-d "$file")
    {
        opendir(newDIR, $file) or die $!;

        print TXT "\n\nFolder" . $file . ": " ;
        while(defined ($file = readdir(newDIR)))
        {
            my @arr = glob "*.fna";
            foreach(@arr)
            {
                print TXT "\t$_";
            }


        }

        closedir(newDIR);
    }

I can't seem to figure out how to print out the contents of each folder (all .fna files) to the txt file.

Last edited by shwang3; 02-24-2010 at 09:26 PM..
# 6  
Old 02-24-2010
seems shell is easier than perl, you can call shell script inside perl

Code:
for i in yourdir/*;do
if [ -d $i ];then
echo ${i##*/}" "`ls $i` | tr " " ","
echo
fi
done

# 7  
Old 04-06-2010
Creating a multidimensional array of subdirectories and its contents

Try this...

Code:
find . -type d -depth -print | grep -v ^.$ | cpio -pd /path

This command will find all subdirectories under current path and create the same under specified path

/Rajiv

Last edited by radoulov; 04-06-2010 at 04:03 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multidimensional array

I am learning about bash system variables, such as $ , @ and #. I have this piece of script implementing an array and it is doing its job just fine. This is not the only array I will be using. Just for ease of maintenance and more coding I would like to have the arrays in two dimensional... (4 Replies)
Discussion started by: annacreek
4 Replies

2. Shell Programming and Scripting

Perl: array name tha contains contents of a variable

Hi there I have a counter called my $counter = 0; I am trying to build an array that will have a name that is for example my @array0 = ("some", "stuff"); but instead of hard coding the "0" in the array name i want to use whatever value the aforementioned $counter has in it...so ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

3. UNIX and Linux Applications

SQL database call into Multidimensional Array using Perl Script

#!/usr/local/bin/perl use DBI; use File::Copy; use Time::Local; use Data::Dumper; -Comments Describing what I'm doing-------------- -I'm pulling information from a database that has an ID and Name. They are separated by a space and I'm trying to load them into a multidimensional array so as... (3 Replies)
Discussion started by: eazyeddie22
3 Replies

4. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

5. Shell Programming and Scripting

perl-data from file save to multidimensional array

i have a file,like 1 3 4 5 6 7 8 9 i want to save it into an array. and then i want to get every element, because i want to use them to calculate. for example: i want to calculate 1 + 3. but i cannot reach my goal. open (FILE, "<", "number"); my @arr; while (<FILE>){ chomp;... (1 Reply)
Discussion started by: pp-zz
1 Replies

6. Shell Programming and Scripting

How to match all array contents and display all highest matched sentences in perl?

Hi, I have an array with 3 words in it and i have to match all the array contents and display the exact matched sentence i.e all 3 words should match with the sentence. Here are sentences. $arr1="Our data suggests that epithelial shape and growth control are unequally affected depending... (5 Replies)
Discussion started by: vanitham
5 Replies

7. Programming

multidimensional array using c++ vector

Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator working,... (0 Replies)
Discussion started by: carl.alv
0 Replies

8. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

9. Shell Programming and Scripting

Awk multidimensional Array

Hello Experts,, Can anybody give me a brief idea what is following bold letter statement is for!! what is the term called so that I can google for it.. It seems to be an array inside another array.. awk' /TXADDR/ { txaddr=$NF } ##understood /TXDATA/ { txdata]=$NF... (1 Reply)
Discussion started by: user_prady
1 Replies

10. Shell Programming and Scripting

multidimensional array in perl

i'm trying to open a file with three or more columns and an undetermined, but finite number of rows. I want to define an array for each row with each element of the row as a sub array. The columns are separated by tabs or spaces. Here's the file: 12x3.12z34b.342sd3.sds 454.23.23.232 ... (9 Replies)
Discussion started by: prkfriryce
9 Replies
Login or Register to Ask a Question