Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I would like to read the names of all the files in a directory of mine into an array so that I can convert each file name into a link to that file.

very simply, use opendir, readdir, and closedir

my $dir = '/'; ## rem trailing slash my $body; ## our file list my $saveFile = '/dev/null'; ## file to save links opendir( MYDIR, $dir ) or die 'opendir'; $body = join( "\n", ## make it legible map { '<a href="$_">$_</a><br>' } ## format each file sort { $a cmp $b } ## sort them grep { ! /^\./ } ## no .name files grep { -T "$dir$_ } ## only text files readdir MYDIR ); closedir MYDIR; ## build your link page ## using $body open( FILE, $saveFile ) or die 'open'; print FILE <<EOF; <HTML> <HEAD> <TITLE>My Files</TITLE> <BODY> $body </BODY> </HTML> EOF close FILE;

see also join, grep, map, and sort
( along with Schwartzian Transform and file test operators )

--jj--


In reply to Re: How do I read all the file names of a directory into an array? by gt8073a
in thread How do I read all the file names of a directory into an array? by Ri-Del

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found