You're very close on this one...
One thing you will see a lot around here - use strict and the -w flag.
One of the things that using the warning flag and the strict pragma would have caught is that you have two different names for the array that you use to contain the files you don't want to list - @nowshow and @noshow. Using strict and the -w flag can save a lot of heartache and headaches in finding problems in your code. Also, if this is a CGI script, you want to look into the -T flag. Take a look at perlsec for more information.

The other problem is with the unless statement. $f eq @noshow won't work. It won't tell you if $f is equal to a value in @noshow. What you need to do is use the grep function. This pulls out information from a list based on the criteria that you set. In the code below, it searches for $f in the list of filenames not to display. If it finds it, it won't print it out.

Finally, another recommendation - use CGI or die. The module CGI.pm provides many, many built in functions for creating CGI scripts. It's invaluable for creating CGI programs.

#!/usr/bin/perl -w print "Content-type: text/html\n\n"; opendir(DIR,"./") || die print "Couldn't open directory"; my @files = readdir(DIR); closedir(DIR); my $filename = __FILE__; my @noshow = (".", "..", $filename); foreach $f (@files) { unless (grep /$f/, @noshow) # look for $f in @noshow { print "$f <br>\n"; } }

Rich36
There's more than one way to screw it up...


In reply to Re: list dir contents, w/o some stuff by Rich36
in thread list dir contents, w/o some stuff by einerwitzen

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.