in reply to Need help with creating a filename only array

Your mistake was, that you prepended to filename and afterwards your regex wouldn't match anymore. Here's a fix:

my @files = (); my $dir = $rootPath . $input->param('path'); $dir .= '/' unless $dir =~ m#/$#; opendir (DIR, $dir) or die $!; @files = grep { -f } map { "$dir$_" } grep { !m#^\.# } readdir (DIR); closedir (DIR);

That's the working version of your code. But all hidden files (on *n?x, that is) are removed, too. If you want them to, all you have to do is remove that regex. "." and ".." will get filtered out as well since they are directories, too. At least on my box it worked all fine. That one line should then look like this:

@files = grep { -f } map { "$dir$_" } readdir (DIR);

Regards,
-octo

Replies are listed 'Best First'.
Re: Re: Need help with creating a filename only array
by S_Shrum (Pilgrim) on Jul 27, 2002 at 07:15 UTC

    That almost works. However, I just need the filename (minus the drive and path):

    g:/this/that/other/somefile.txt needs to go into the array as somefile.txt

    Your code above, while it gets rid of folders (including '.', '..'), puts the drive, path, and filename into the array (like above).

    How do I get just the filename?

    TIA

    ======================
    Sean Shrum
    http://www.shrum.net

      Oh.. Well, I tried to stick as close to your code as possible. This should work for you though:

      @files = grep { -f "$dir$_" } readdir (DIR);

      Simple, eh? ;)

      Regards,
      -octo

        Damn that was simple. I knew I was overthinking this thing. Perfect. Thanks again.

        ======================
        Sean Shrum
        http://www.shrum.net