Home > Archive > PERL Beginners > March 2008 > How to make all files in one directory to an array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to make all files in one directory to an array
|
|
| Sivasakthi 2008-03-20, 8:02 am |
| Hi all,
How to form array by using the files available in particular directory ?
for example, /tmp contains the following files..
#ls /tmp
example.txt
file1.txt
file2.txt
file3.txt
sample.txt
www.txt
zzz.txt
i want to take all the above files in to one array? how to do that?
Thanks,
Siva
| |
| Chas. Owens 2008-03-20, 8:02 am |
| On Thu, Mar 20, 2008 at 8:50 AM, sivasakthi <msivasakthi@gmail.com> wrote:
> Hi all,
>
> How to form array by using the files available in particular directory ?
snip
my @list_of_files = </tmp/*>;
However, this is not a good idea. Do you know how many files are in
that directory? If it is an extraordinarily large directory with
large file names you could eat up a lot of memory. Most of the time
you want to process each file in a directory by itself, so you don't
need an array, just a way to iterated over the items in the directory.
Perl provides the opendir*, readdir**, closedir*** functions to let
you do this.
* http://perldoc.perl.org/functions/opendir.html
** http://perldoc.perl.org/functions/readdir.html
*** http://perldoc.perl.org/functions/closedir.html
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
| |
| Ken Foskey 2008-03-20, 7:02 pm |
|
On Thu, 2008-03-20 at 18:20 +0530, sivasakthi wrote:
> Hi all,
>
> How to form array by using the files available in particular directory ?
>
> for example, /tmp contains the following files..
>
> #ls /tmp
> example.txt
> file1.txt
> file2.txt
> file3.txt
> sample.txt
> www.txt
> zzz.txt
>
> i want to take all the above files in to one array? how to do that?
Off the top of my head:
chdir( '/tmp' );
@files = glob('*');
perldoc -f glob
| |
| gulsuner@bilkent.edu.tr 2008-03-20, 7:02 pm |
| Hi,
You can use opendir, readdir and may play with grep as below;
opendir $dir, '/tmp' or
@list = grep ! /^\.\.?$/, readdir($dir);
grep to remove '.' and '..'
@list = grep ! /^\.+/ # you can remove '.' , '..' and hidden files
@list = grep /\.txt$/ # only txt files
etc....
suleyman
On Thu, 2008-03-20 at 18:20 +0530, sivasakthi wrote:
> Hi all,
>
> How to form array by using the files available in particular directory ?
>
> for example, /tmp contains the following files..
>
> #ls /tmp
> example.txt
> file1.txt
> file2.txt
> file3.txt
> sample.txt
> www.txt
> zzz.txt
>
> i want to take all the above files in to one array? how to do that?
>
>
> Thanks,
> Siva
>
>
| |
| Chas. Owens 2008-03-20, 7:02 pm |
| On Thu, Mar 20, 2008 at 9:16 AM, S=FCleyman G=FCls=FCner
<gulsuner@bilkent.edu.tr> wrote:
> Hi,
>
> You can use opendir, readdir and may play with grep as below;
>
> opendir $dir, '/tmp' or
> @list =3D grep ! /^\.\.?$/, readdir($dir);
> grep to remove '.' and '..'
snip
This isn't anymore efficient than saying
my @list =3D grep { ! /^\.\.?$/ } </tmp/*>, </tmp/.*>;
If you are going to use readdir, use it in a loop:
my @list;
while (readdir $dir) {
push @list, $_ unless /^\.\.?$/;
}
--=20
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
|
|
|
|
|