in reply to Re: Get file names
in thread Get file names

Yes.. i´ve already use that..!! Is this the best way or the most efficent way? Or is preferably to use modules instead of perl built in commands?

I´ve made this code:

use strict; use warnings; my $path="c:\\teste"; sub procura_ficheiros{ ($path)=@_; opendir(DIR, $path) ||die "$!"; my @files=grep{/\.c$/} readdir(DIR); close (DIR); return @files; } my @res=procura_ficheiros($path); foreach my $fich (@res){ my $final=$path."\\".$fich; print "Ficheiro: $path\\$fich\n"; print "Final: $final\n"; }

It works.. but is it the most efficcient??

Thank you once again

Nuno

Replies are listed 'Best First'.
Re: Re: Re: Get file names
by Preceptor (Deacon) on Aug 26, 2003 at 19:11 UTC
    You might want to have a look at the 'glob' function. It allows a wildcard expression and puts the results in an array.
    eg. (yes, it's cygwin, so YMMV)
    #!/bin/perl use strict; use warnings; foreach (glob ( "/cygdrive/c/temp/*" )) { print $_, "\n"; }

    Not amazingly _efficient_ (or so I'd imagine, I haven't profiled glob) but pretty straightforward code.
Re: Re: Re: Get file names
by tcf22 (Priest) on Aug 26, 2003 at 18:11 UTC
    Try using Benchmark to test the efficiency. I tried it, but I couldn't get broquaint's code to work. I was getting and empty directory listing. It may be a problem over here, haven't really looked into it yet.