UPDATE 1-8-03 - I have posted the finished version to the end of this post so others may happen upon it when in need.

Ahhh...more intro Perl homework goodness. :)

So far, with your kind assistance, I have written;

#!/usr/bin/perl use warnings; use strict; open (OUTFILE, ">>midterm.html"); my $dir = "/Users/ctp/PERL_work"; my @dirs; my @files; # read all entries from dir and skip '.' and '..' directories opendir (DIRHANDLE, $dir) or die "can't open $dir: $!"; my @list=grep !/^\.\.?\z/, readdir DIRHANDLE; print (OUTFILE "<HTML>\n<HEAD></HEAD>\n<BODY>\n"); # now we sort the list on directory basis foreach my $file (sort {-d "$dir/$b" <=> -d "$dir/$a"} @list) { print "<B>$file</B><BR>\n" if -d "$dir/$file"; print "$file<BR>\n" if -T "$dir/$file"; } closedir (DIRHANDLE); print @dirs; print (OUTFILE "</BODY>\n</HTML>\n"); close (OUTFILE);
Inside the foreach loop I have two print statements. I want that output to also go to my OUTFILE. I have tried printing them to the filehandle directly, I have tried pushing the output into an array, I have tried a few other things, all of which produced syntax errors. None of my books have any examples that are even close...not that I can find anyway.

BTW - In my previous (part one) query an anonymous monk suggested perldoc -f open and perldoc-f select. After poring over perldoc -f in three books I'm still; not getting how to make use of that one...not that that's what I want to do or not do necessarily, but it was brought up

Thanks again!

PS - as always, since this is homework, if you know of a good example please direct me to it (print or online) so I can read up!

--begin final version--
#!/usr/bin/perl use warnings; use strict; #open file for output open (OUTFILE, ">>dircontents.html"); #declare $dir variable and set our directory my $dir = "/Users/ctp/PERL_work"; # create dir handle read all entries from directory opendir (DIRHANDLE, $dir) or die "can't open $dir: $!"; #skip "." and ".." directories my @list=grep !/^\.\.?\z/, readdir DIRHANDLE; #print HTML header to our file print (OUTFILE "<HTML>\n<HEAD></HEAD>\n<BODY>\n"); # sort the list on directory basis and close dir handle foreach my $file (sort {-d "$dir/$b" <=> -d "$dir/$a"} @list) { print (OUTFILE "<B>$file</B><BR>\n") if -d "$dir/$file"; print (OUTFILE "$file<BR>\n") if -T "$dir/$file"; } closedir (DIRHANDLE); #print HTML footer to file and close filehandle print (OUTFILE "</BODY>\n</HTML>\n"); close (OUTFILE);

In reply to reading files and directories, part two by ctp

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.