array and files

R

Robin

I want to read files from a directory into an array and then parse the
contents of these files into one array, anyone brave enough, sorry if I
don't have a partial answer, I like what jim wrote under my group posting,
but I don't know where to start, push and pop function??
 
P

Paul Lalli

One method would be to use the special properties of <>:

opendir DIR, "$directory" or die "Cannot open directory: $!";
@ARGV = grep {! -d} readdir DIR;
closedir DIR;
@all_contents = <>;


This would open whatever directory is in $directory, read all files from
that directory (excluding any directories) into @ARGV, and then read the
contents of each of those file into one large array called @all_contents.

see also the I/O section of perldoc perlop and the section on @ARGV from
perldoc perlvar

Paul Lalli
 
T

Tore Aursand

I want to read files from a directory into an array and then parse the
contents of these files into one array, anyone brave enough, sorry if I
don't have a partial answer [...]

This code should get you going (untested):

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

# Create a list of the contents in the directory
opendir( DIR, '/directory' ) || or die "$!\n";
my @files = readdir( DIR );
closedir( DIR );

# Process each file (...) found
foreach ( @files ) {
# Skip non-files or files which aren't readable by the effective
# uid/gid. See 'perldoc -f -X' for more information.
next unless ( -f && -r );

# Open the file and iterate over each line
if ( open(TXT, $_) ) {
while ( <TXT> ) {
# ...
}
close( TXT );
}
else {
print "Couldn't open '$_'; $!\n";
}
}
Please CC a copy of your message to me.

No. This is Usenet.
 
J

James Willmore

I want to read files from a directory into an array and then parse the
contents of these files into one array, anyone brave enough, sorry if I
don't have a partial answer, I like what jim wrote under my group
posting, but I don't know where to start, push and pop function??

Thanks for the comment :)

These command line commands should get you started ...

prompt> perldoc -f opendir
prompt> perldoc -f readdir
prompt> perldoc -f push
prompt> perldoc -f pop
prompt> perldoc File::Find

(I *think* File::Find is a part of the core Perl distrobution - someone
correct me if I'm wrong)

Others have posted other examples and direction.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
BOO! We changed Coke again! BLEAH! BLEAH!
 
J

James Willmore

I want to read files from a directory into an array and then parse the
contents of these files into one array, anyone brave enough, sorry if I
don't have a partial answer, I like what jim wrote under my group posting,
but I don't know where to start, push and pop function??

Thanks for the comment :)

These command line commands should get you started ...

prompt> perldoc -f opendir
prompt> perldoc -f readdir
prompt> perldoc -f push
prompt> perldoc -f pop
prompt> perldoc File::Find

(I *think* File::Find is a part of the core Perl distrobution - someone
correct me if I'm wrong)

Others have posted other examples and direction.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
If you live in a country run by committee, be on the committee.
-- Graham Summer
 
T

Tassilo v. Parseval

Also sprach Robin:
I want to read files from a directory into an array and then parse the
contents of these files into one array, anyone brave enough, sorry if I
don't have a partial answer, I like what jim wrote under my group posting,
but I don't know where to start, push and pop function??

A quick way might be:

local @ARGV = grep -f, glob "/dir/*";
my @content = <>;

Now you have the content of all files in the directory /dir (possibly
sans hidden ones, if you want those use '/dir/.?*' as pattern) in one
array.

Note that this could become a challenge for your memory. It's seldom
necessary to slurp a whole into memory, even less so many of them.

Tassilo
 
T

Tore Aursand

(I *think* File::Find is a part of the core Perl distrobution - someone
correct me if I'm wrong)

You're not wrong, at least not for Perl 5.8.2. 'perldoc perlmodlib' gives
you a list of modules that comes with the core Perl distribution.
 
T

Tassilo v. Parseval

Also sprach Tore Aursand:
You're not wrong, at least not for Perl 5.8.2. 'perldoc perlmodlib' gives
you a list of modules that comes with the core Perl distribution.

File::Find has been a standard modules for ages. The oldest Perl release
I can find it in on the CPAN is actually 5.003_07. And this one is from
1996.

Tassilo
 
T

Tad McClellan

Paul Lalli said:
One method would be to use the special properties of <>:

opendir DIR, "$directory" or die "Cannot open directory: $!";
^ ^


Only without that Useless Use of Double Quotes.



[ snip TOFU ]
 
A

Anno Siegel

Paul Lalli said:
One method would be to use the special properties of <>:

opendir DIR, "$directory" or die "Cannot open directory: $!";
@ARGV = grep {! -d} readdir DIR;
closedir DIR;
@all_contents = <>;

Perhaps a good idea (if you don't mind recklessly bumping over unreadable
files), but the implementation is buggy. You didn't test this, did you?

@ARGV = map "$directory/$_", grep ! -d, readdir DIR;

or you will (try to) read the wrong files.

Anno
 
B

Ben Morrow

Perhaps a good idea (if you don't mind recklessly bumping over unreadable
files), but the implementation is buggy. You didn't test this, did you?

@ARGV = map "$directory/$_", grep ! -d, readdir DIR;

Or rather,

use File::Spec::Functions;
@ARGV = map { rel2abs $_, $directory } grep ! -d, readdir DIR;

Ben
 

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,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top