in reply to getting files from Dir

I use a different approach:

opendir DH, "/directory"; @files = readdir DH; closedir DH; foreach (@files) { /tim(\d*)/; $num = $1; if ( ($num > 14 ) && ( $num < 51) {push @wanted, $_}; }

Code is untested, take your chances. ObBrag: I can make it smaller, but that's harder to read.

Update: Changes made as suggested by davis - thanks. Personally I think open and opendir should be unified, but I guess there's some good reason why not.

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re: Re: getting files from Dir
by davis (Vicar) on Jan 24, 2003 at 10:01 UTC
    open DH, "/directory";
    @files = readdir DH;
    close DH;
    I'd imagine you want to change the "open" and "close" to "opendir" and "closedir" respectively
    cheers
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Re^2: getting files from Dir
by Aristotle (Chancellor) on Jan 24, 2003 at 16:55 UTC
    Why don't you check your match for success? If if fails, you get the $1 from the previous iteration. That's probably not what you intended. Also, I'd use \d+ instead of \d* since we're not interested in matching tim without any following digits. It probably should be anchored to the front of the string too, by the OP's spec.
    my @wanted; opendir my $dh, $dir or die "Failed opening $dir: $!"; foreach (readdir $dh) { next unless /^tim(\d+)/; my $num = $1; push @wanted, $_ if $num > 14 and $num < 51; } closedir $dh;
    Personally, I'd wrap the whole thing in a do block and use grep, so that I don't need to track my handles explicitly:
    my @tim_file = do { opendir my $dh, $dir or die "Failed opening $dir: $!"; grep { my ($num) = /^tim(\d+)/; defined $num and $num > 14 and $num < 51; } readdir $dh; };
    Note that when you assign a match's captures to a list, failed expressions will return undef, so here I can implicitly test for success by testing the definedness of $num.

    Makeshifts last the longest.