Simple file list in directory to array

D

darkmoo

Thanks to all that have helped with my previous question.

Now one more question. I've completely forgetten how to grab a list of all
files in a directory and load it up into an array.

Im not a full time perl programmer so always returning back to te learning
curve on the same topics :(

Any help would be greatly appreciated.
 
R

Reinhard Pagitsch

darkmoo said:
Thanks to all that have helped with my previous question.

Now one more question. I've completely forgetten how to grab a list of all
files in a directory and load it up into an array.

Im not a full time perl programmer so always returning back to te learning
curve on the same topics :(

Any help would be greatly appreciated.

Look at opendir,readdir,closedir or use the module File::Find (have to
fill the array _manualy_).

regards,
Reinhard
 
T

Thomas J.

darkmoo said:
Now one more question. I've completely forgetten how to grab a list of all
files in a directory and load it up into an array.

perl -e 'opendir($d,".");print join "\n", grep -f,readdir($d); close
$d;'
Im not a full time perl programmer so always returning back to te learning
curve on the same topics :(

There is a great way where you can start to get information about
perlfunctions:

perldoc perlfunc

the functions are arranged according to topics

Thomas
 
D

Dr.Ruud

Jim Gibson schreef:
darkmoo:

In addition to the other, more recommended ways, there is always:

my @array = `ls /my/directory`;

If ls is available, and directories and links are welcome.
 
U

usenet

darkmoo said:
Now one more question. I've completely forgetten how to grab a list of all
files in a directory and load it up into an array.

Good. That's a good thing to forget, because it's almost always a bad
idea (and reflects poor programming practice).

You are probably thinking of doing something ill-advised such as:

my @files = [something that gets a list of files];

foreach my $file( @files ) {
# do something with $file
}

That approach is not good. You should instead skip the array and do
something like this:

foreach my $file( [something that gets a list of files] ) {
# do something with $file
}

You should never (or, at least, very rarely) load up a data structure
(such as an array) and then iterate over the structure. It is (almost)
always better to simply iterate over whatever process loads up the
array (bypassing the need for the array). There are exceptions, such
as if you have a valid need to iterate over the structure more than
once (but things like that should be very uncommon - usually that would
also reflect an error in logic).

Others have given you references to [something that gets a list of
files]. If you are fortunate enough to be using *NIX, though, you might
also consider IO::All. For example, to iterate over all *.txt (case
insensitive) files recursed no more than two directories deep which are
smaller than 1000 bytes and whose first line contains the string
"spooler" (case sensitive):

use strict; use warnings;
use IO::All;

my $root_dir = "/tmp";
foreach my $file (
io($root_dir) -> filter(sub { $_->name =~ /\.txt$/i
&& $_->size < 1000
&& ($_->head)[0] =~ /spooler/
} )
-> all_files(2);
) {
#do something with $file
}

__END__
 
U

usenet

A. Sinan Unur said:
In both cases, processing won't begin until the list has been
constructed.

Of course this is true (for this example). The difference is whether or
not you create an unnecessary named element in your namespace. Not
doing so reduces the chance of typos (which, granted, strict() should
detect) or logic errors, such as improperly scoped but identically
named structures (such as the ever ubiquitous @data) which strict()
won't detect.

Sometimes programming clarity compels us to create redundant named data
structures. But I prefer to avoid these whenever possible. It is
certinally not necessary to create such a redundant named structure in
the example I cited, and is contrary to conventional Perl programming
practice (which prefers "while (<DATA>)" to "for (@DATA)").
 
A

anno4000

Dr.Ruud said:
Jim Gibson schreef:

If ls is available, and directories and links are welcome.

....plus line-feeds at the end of each file name (a *very* popular trap).

chomp( my @array = `ls ...`);

comes closer.

Anno
 
A

A. Sinan Unur

(e-mail address removed) wrote in @b28g2000cwb.googlegroups.com:
Of course this is true (for this example). The difference is whether or
not you create an unnecessary named element in your namespace.

OK, I was more focused on not creating the list.

That is one reason why I still stick with File::Find. When I had to write
something to organize my digital photos (all 30000 or so of them, most of
them should be deleted probably), it was really much more satisfiying when
the processing began immediately (I am not sure if there was an overall
performance difference though, I did not measure it).
But I prefer to avoid these whenever possible. It is
certinally not necessary to create such a redundant named structure in
the example I cited, and is contrary to conventional Perl programming
practice (which prefers "while (<DATA>)" to "for (@DATA)").

The same convention would indicate using File::Find, wouldn't it?

Sinan
 
A

A. Sinan Unur

That approach is not good. You should instead skip the array and do
something like this:

foreach my $file( [something that gets a list of files] ) {
# do something with $file
}

Wait a minute ;-)

The list will still be constructed in its entirety before the loop.

I don't think his remark was as much about constructing the list as a
whole as opposed to using an iterator, as much as it is about avoiding
unnecessary intermediate variables:

I got it. Thank you.

Sinan
 
T

Tad McClellan

darkmoo said:
I've completely forgetten how to grab a list of all
files in a directory and load it up into an array.


my @files = grep -f, glob '* .*';
 
U

usenet

A. Sinan Unur said:
That is one reason why I still stick with File::Find.

I believe you will find that File::Find and IO::All behave the same way
when you do the same sorts of things. That's because IO::All is mearly
a proxy for File::Find (and several other modules). IO::All does
almost nothing itself except provide a consistent OO front-end to a
bunch of builtins and modules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top