Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to read contents of directory into an array?

by Anonymous Monk
on Aug 06, 2001 at 18:17 UTC ( [id://102460]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

Im a novice to PERL and find the Active PERL API conventions somewhat confusing. Im trying to read the contents of a directory into an array for processing.

Can anyone help me....................?????

Pretty please with sugar on top....

Thanks v. much.

Pete.

Edit by tye

  • Comment on How to read contents of directory into an array?

Replies are listed 'Best First'.
Re: Help Me Please!
by c-era (Curate) on Aug 06, 2001 at 18:20 UTC
    You should lookup the functions opendir and readdir. Here is an example:
    my @files; opendir (DIR,"my/dir") || die $!; @files = readdir (DIR); closedir (DIR) || die $!;
Re: Help Me Please!
by physi (Friar) on Aug 06, 2001 at 18:24 UTC
    look at  perldoc -f opendir and perldoc -f readdir
    This should help you.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: Help Me Please!
by kilinrax (Deacon) on Aug 06, 2001 at 18:22 UTC
    The function you're probably looking for is readdir.
Re: Help Me Please!
by IraTarball (Monk) on Aug 06, 2001 at 18:58 UTC
    You might find glob() usefull. Or maybe it's alias <*>. This let's you get the current directory contents with something like:
    my @dir = <*>;

    You can use the glob() function to get the contents of a directory other than the current directory.

    Have a look at perldoc -f glob for more info.

    Ira,

    "So... What do all these little arrows mean?"
    ~unknown

Re: How to read contents of directory into an array?
by TStanley (Canon) on Aug 07, 2001 at 02:04 UTC
    The File::Slurp module from CPAN will also do what you require.
    #!/usr/bin/perl -w use strict; use File::Slurp; my $DIR="directory"; my @Files=read_dir($DIR); my $ln=@Files; for(my $x=0;$x<$ln;$x++){ print"File: $Files[$x]\n"; }
    This module is also available for ActiveState Perl as well, by using the PPM.

    TStanley
    --------
    There's an infinite number of monkeys outside who want to talk to us
    about this script for Hamlet they've worked out
    -- Douglas Adams/Hitchhiker's Guide to the Galaxy

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://102460]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found