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

I have a script opening a directory and stores the names off all the files in the directory (.html .php .gif .png ...) and then a foreach command that print off each name as a link to that file online.

what I want to do is add something to the unless part of the foreach command that says to print off the link unless it has an extension of .gif . Maybe add a bit to the @noshow ? I don't know :)

Here it is:
opendir(DIR, $directory) || die print "Couldn't open directory"; my @unscont = readdir(DIR); closedir(DIR); my @unfiles = grep(/\./, @unscont); my @undirs = grep(!/\./, @unscont); @files = sort(@unfiles); @dirs = sort(@undirs); my @noshow = (".", ".."); foreach $f (@files) { unless (grep /$f/, @noshow) { print " <a style=\"{font-size: 13px; color: #000000;\"} href=\"./$ +this?func=read&file=$f&dire=$dire\">$f</a><br>\n"; } }

Does that make sense? I know it's kind of primitave but I am just tryin to get the hang of it all here :)

Thanks, everyone!

Replies are listed 'Best First'.
Re: noshow by file extension
by Aristotle (Chancellor) on Jun 03, 2002 at 06:34 UTC
    opendir(DIR, $directory) || die print "Couldn't open directory"; my @unscont = sort grep !/^\.\.?$/ && !/\.gif$/, readdir(DIR); closedir(DIR); @files = grep(/\./, @unscont); @dirs = grep(!/\./, @unscont); for my $file (@files) { print qq[ <a style="{font-size: 13px; color: #000000;"} href="./$ +this?func=read&file=$file&dire=$dire">$file</a><br>\n]; }
    Note the use of grep to sort out all entries consisting of a single or double dot and those ending in .gif right off the bat, then shuffling it through sort so that you don't have to call sort twice to sort the lists of files and directories individually. You can also write that separation into @files and @dirs as (/\./ ? push @files, $_ : push @dirs, $_) for @unscont;thus finishing the job with a single pass through the list. Lastly, /\./ is a really lousy form of checking for directories because who's to say a file has to have a dot in its name, or that a directory cannot have one? Instead, use the -d operator as in
    my @dirs = grep -d "$directory/$_", @unscont; my @files = grep ! -d "$directory/$_", @unscont;
    (Or as part of my push proposition; that shall be left as an excercise to the reader.)

    Makeshifts last the longest.

      my @unscont = sort grep !/^\.\.?$/ && !/\.gif$/, readdir(DIR);

      Bad advice. If that grep !/^\.\.?$/ is there to remove the current and parent directories, it won't always work.

      Trying to remove these directories from the output of readdir with a regexp is hard to get right (and when you get it right it's damned ugly). Use procedural code instead. It's clearer and has no borderline cases to trip you up.

      my @unscont = sort grep { $_ ne '.' and $_ ne '..' and !/\.gif$/ } readdir(DIR);
      my @dirs = grep -d "$directory/$_", @unscont; my @files = grep ! -d "$directory/$_", @unscont;
      (Or as part of my push proposition; that shall be left as an excercise to the reader.)

      If you want to separate files and dirs in one pass, you could do something like:

      my( @dirs, @files ); push @{ -d "$directory/$_" ? \@dirs : \@files}, $_ for @unscont;

      That is, I could admit to doing that, but I'm not sure that it's crystal clear in its intent :)


      print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
        Thanks for the corrections - esp for the grep-regex pitfall. Duh at myself for the push bit (putting it in mere round brackets and returning simple arrays doesn't work; should have thought of return refs and dereferencing them). Yes, it's a bit unclear, but esp when we're talking about a CGI that may have to handle large directories and somewhat substantial traffic I'd prefer to use it this and stick a comment in to explain it instead.

        Makeshifts last the longest.

Re: noshow by file extension
by Beatnik (Parson) on Jun 03, 2002 at 06:22 UTC
    opendir(DIR, $directory) || die print "Couldn't open directory"; my @stuff = readdir(DIR); closedir(DIR); shift @stuff; shift @stuff; @dirs = sort(@undirs); foreach $f (@files) { print qq| <a style="{font-size: 13px; color: #000000;"} href="./$th +is?func=read&file=$f&dire=$dire">$f</a><br>\n|; }
    Just a guess...
    grep can be quite slow and it's kinda useless there :)
    <RB> Probably not entirely what you want tho :|

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: noshow by file extension
by Zaxo (Archbishop) on Jun 03, 2002 at 11:14 UTC

    I think glob would do a lot of the work for you here, as would CGI.pm. Example:

    use CGI qw(:html4); chdir "$ENV{'DOCUMENT_ROOT'}/foo"; my @toshow = grep {-f} # filter to only regular files glob('*.{png,jpg,html}'); print map { a({ -style => q(Color: #000000; font-size: 13px;), -href => "/foo/$_" }, $_ ) } @toshow;
    I use that map because the file name is repeated in href and text. You may need to chdir or tune the file names to make the urls come out right. I have "/foo" tacked on to illustrate.

    After Compline,
    Zaxo

Re: noshow by file extension
by kpo (Initiate) on Jun 03, 2002 at 06:41 UTC
    i wasnt too sure about why you obtained the @dirs, but other than that this seems to work pretty well.
    (I would also look into replacing grep...)
    opendir(DIR, $directory) || die print "Couldn't open directory"; my @unscont = readdir(DIR); closedir(DIR); ## Sorts and Extracts in one statement(each) my @files = sort grep(/\./, @unscont); my @dirs = sort grep(!/\./, @unscont); ## Removes 1st two elements '.' & '..' @files = @files[2..$#files]; my @noshow = (".py", ".html"); foreach $f (@files) { my $switch = true; foreach $n (@noshow) { if($f =~ /$n$/) { $switch = ''; } } if($switch) { print " <a href=\"./this?func=read&file=$f&dire=$dire\">$f</a><br +>\n"; } }
      ## Removes 1st two elements '.' & '..' @files = @files[2..$#files];

      Bad advice. That's a dangerous way of removing those two elements, as merlyn pointed out a while ago, in the thread Is readdir ever deterministic?.


      print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'